CMS 3D CMS Logo

Herwig7Interface.cc
Go to the documentation of this file.
1 
7 #include <cmath>
8 #include <cstdlib>
9 #include <fstream>
10 #include <iostream>
11 #include <memory>
12 #include <sstream>
13 
14 #include <algorithm>
15 
16 #include <HepMC/GenEvent.h>
17 #include <HepMC/PdfInfo.h>
18 #include <HepMC/IO_GenEvent.h>
19 
20 #include <Herwig/API/HerwigAPI.h>
21 
22 #include <ThePEG/Utilities/DynamicLoader.h>
23 #include <ThePEG/Repository/Repository.h>
24 #include <ThePEG/Handlers/EventHandler.h>
25 #include <ThePEG/Handlers/XComb.h>
26 #include <ThePEG/EventRecord/Event.h>
27 #include <ThePEG/EventRecord/Particle.h>
28 #include <ThePEG/EventRecord/Collision.h>
29 #include <ThePEG/EventRecord/TmpTransform.h>
30 #include <ThePEG/Config/ThePEG.h>
31 #include <ThePEG/PDF/PartonExtractor.h>
32 #include <ThePEG/PDF/PDFBase.h>
33 #include <ThePEG/Utilities/UtilityBase.h>
34 #include <ThePEG/Vectors/HepMCConverter.h>
35 
37 
39 
43 
44 #include "CLHEP/Random/RandomEngine.h"
45 
46 using namespace std;
47 using namespace gen;
48 
50  : randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
51  dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
52  generator_(pset.getParameter<string>("generatorModule")),
53  run_(pset.getParameter<string>("run")),
54  dumpConfig_(pset.getUntrackedParameter<string>("dumpConfig", "HerwigConfig.in")),
55  skipEvents_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0)) {
56  // Write events in hepmc ascii format for debugging purposes
57  string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
58  if (!dumpEvents.empty()) {
59  iobc_ = std::make_unique<HepMC::IO_GenEvent>(dumpEvents, ios::out);
60  edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
61  }
62  // Clear dumpConfig target
63  if (!dumpConfig_.empty())
64  ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
65 }
66 
68  if (eg_)
69  eg_->finalize();
70  edm::LogInfo("Herwig7Interface") << "Event generator finalized";
71 }
72 
73 void Herwig7Interface::setPEGRandomEngine(CLHEP::HepRandomEngine *v) {
74  randomEngineGlueProxy_->setRandomEngine(v);
75  randomEngine = v;
77  if (rnd) {
78  rnd->setRandomEngine(v);
79  }
80 }
81 
83  std::string runModeTemp = pset.getUntrackedParameter<string>("runModeList", "read,run");
84 
85  // To Lower
86  std::transform(runModeTemp.begin(), runModeTemp.end(), runModeTemp.begin(), ::tolower);
87 
88  while (!runModeTemp.empty()) {
89  // Split first part of List
90  std::string choice;
91  size_t pos = runModeTemp.find(',');
92  if (std::string::npos == pos)
93  choice = runModeTemp;
94  else
95  choice = runModeTemp.substr(0, pos);
96 
97  if (pos == std::string::npos)
98  runModeTemp.erase();
99  else
100  runModeTemp.erase(0, pos + 1);
101 
102  HwUI_.reset(new Herwig::HerwigUIProvider(pset, dumpConfig_, Herwig::RunMode::READ));
103  edm::LogInfo("Herwig7Interface") << "HerwigUIProvider object with run mode " << HwUI_->runMode() << " created.\n";
104 
105  // Chose run mode
106  if (choice == "read") {
108  HwUI_->setRunMode(Herwig::RunMode::READ, pset, dumpConfig_);
109  edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
110  << " will be passed to Herwig for the read step.\n";
112  } else if (choice == "build") {
114  HwUI_->setRunMode(Herwig::RunMode::BUILD, pset, dumpConfig_);
115  edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
116  << " will be passed to Herwig for the build step.\n";
118 
119  } else if (choice == "integrate") {
120  std::string runFileName = run_ + ".run";
121  edm::LogInfo("Herwig7Interface") << "Run file " << runFileName
122  << " will be passed to Herwig for the integrate step.\n";
123  HwUI_->setRunMode(Herwig::RunMode::INTEGRATE, pset, runFileName);
125 
126  } else if (choice == "run") {
127  std::string runFileName = run_ + ".run";
128  edm::LogInfo("Herwig7Interface") << "Run file " << runFileName << " will be passed to Herwig for the run step.\n";
129  HwUI_->setRunMode(Herwig::RunMode::RUN, pset, runFileName);
130  } else {
131  edm::LogInfo("Herwig7Interface") << "Cannot recognize \"" << choice << "\".\n"
132  << "Trying to skip step.\n";
133  continue;
134  }
135  }
136 }
137 
139  try {
140  edm::LogInfo("Herwig7Interface") << "callHerwigGenerator function invoked with run mode " << HwUI_->runMode()
141  << ".\n";
142 
143  // Call program switches according to runMode
144  switch (HwUI_->runMode()) {
145  case Herwig::RunMode::INIT:
147  break;
148  case Herwig::RunMode::READ:
150  break;
151  case Herwig::RunMode::BUILD:
153  break;
154  case Herwig::RunMode::INTEGRATE:
155  Herwig::API::integrate(*HwUI_);
156  break;
157  case Herwig::RunMode::MERGEGRIDS:
158  Herwig::API::mergegrids(*HwUI_);
159  break;
160  case Herwig::RunMode::RUN: {
161  HwUI_->setSeed(randomEngine->getSeed());
162  eg_ = Herwig::API::prepareRun(*HwUI_);
163  break;
164  }
166  edm::LogError("Herwig7Interface") << "Error during read in of command line parameters.\n"
167  << "Program execution will stop now.";
168  return;
169  default:
170  HwUI_->quitWithHelp();
171  }
172 
173  return;
174 
175  } catch (ThePEG::Exception &e) {
176  edm::LogError("Herwig7Interface") << ": ThePEG::Exception caught.\n"
177  << e.what() << '\n'
178  << "See logfile for details.\n";
179  return;
180  } catch (std::exception &e) {
181  edm::LogError("Herwig7Interface") << ": " << e.what() << '\n';
182  return;
183  } catch (const char *what) {
184  edm::LogError("Herwig7Interface") << ": caught exception: " << what << "\n";
185  return;
186  }
187 }
188 
190  if (HwUI_->runMode() == Herwig::RunMode::RUN) {
191  edm::LogInfo("Herwig7Interface") << "Starting EventGenerator initialization";
193  edm::LogInfo("Herwig7Interface") << "EventGenerator initialized";
194 
195  // Skip events
196  for (unsigned int i = 0; i < skipEvents_; i++) {
198  eg_->shoot();
199  edm::LogInfo("Herwig7Interface") << "Event discarded";
200  }
201 
202  return true;
203 
204  } else {
205  edm::LogInfo("Herwig7Interface") << "Stopped EventGenerator due to missing run mode.";
206  return false;
207  /*
208  throw cms::Exception("Herwig7Interface")
209  << "EventGenerator could not be initialized due to wrong run mode!" << endl;
210 */
211  }
212 }
213 
215  /*ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
216 
217  if (!rnd)
218  edm::LogWarning("ProxyMissing")
219  << "ThePEG not initialised with RandomEngineGlue.";
220  else
221  rnd->flush();
222  */
223 }
224 
225 unique_ptr<HepMC::GenEvent> Herwig7Interface::convert(const ThePEG::EventPtr &event) {
226  return std::unique_ptr<HepMC::GenEvent>(ThePEG::HepMCConverter<HepMC::GenEvent>::convert(*event));
227 }
228 
229 double Herwig7Interface::pthat(const ThePEG::EventPtr &event) {
230  using namespace ThePEG;
231 
232  if (!event->primaryCollision())
233  return -1.0;
234 
235  tSubProPtr sub = event->primaryCollision()->primarySubProcess();
236  TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(sub->incoming()));
237 
238  double pthat = (*sub->outgoing().begin())->momentum().perp() / ThePEG::GeV;
239  for (PVector::const_iterator it = sub->outgoing().begin(); it != sub->outgoing().end(); ++it)
240  pthat = std::min<double>(pthat, (*it)->momentum().perp() / ThePEG::GeV);
241 
242  return pthat;
243 }
244 
246  /* Initialize the input config for Herwig from
247  * 1. the Herwig7 config files
248  * 2. the CMSSW config blocks
249  * Writes them to an output file which is read by Herwig
250  */
251 
252  stringstream logstream;
253 
254  // Contains input config passed to Herwig
255  stringstream herwiginputconfig;
256 
257  // Define output file to which input config is written, too, if dumpConfig parameter is set.
258  // Otherwise use default file HerwigConfig.in which is read in by Herwig
259  ofstream cfgDump;
260  cfgDump.open(dumpConfig_.c_str(), ios_base::app);
261 
262  // Read Herwig config files as input
263  vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
264  // Loop over the config files
265  for (const auto &iter : configFiles) {
266  // Open external config file
267  ifstream externalConfigFile(iter);
268  if (externalConfigFile.is_open()) {
269  edm::LogInfo("Herwig7Interface") << "Reading config file (" << iter << ")" << endl;
270  stringstream configFileStream;
271  configFileStream << externalConfigFile.rdbuf();
272  string configFileContent = configFileStream.str();
273 
274  // Comment out occurence of saverun in config file since it is set later considering run and generator option
275  string searchKeyword("saverun");
276  if (configFileContent.find(searchKeyword) != std::string::npos) {
277  edm::LogInfo("Herwig7Interface") << "Commented out saverun command in external input config file(" << iter
278  << ")" << endl;
279  configFileContent.insert(configFileContent.find(searchKeyword), "#");
280  }
281  herwiginputconfig << "# Begin Config file input" << endl
282  << configFileContent << endl
283  << "# End Config file input";
284  edm::LogInfo("Herwig7Interface") << "Finished reading config file (" << iter << ")" << endl;
285  } else {
286  edm::LogWarning("Herwig7Interface") << "Could not read config file (" << iter << ")" << endl;
287  }
288  }
289 
290  edm::LogInfo("Herwig7Interface") << "Start with processing CMSSW config" << endl;
291  // Read CMSSW config file parameter sets starting from "parameterSets"
292  ParameterCollector collector(pset);
294  iter = collector.begin();
295  herwiginputconfig << endl << "# Begin Parameter set input\n" << endl;
296  for (; iter != collector.end(); ++iter) {
297  herwiginputconfig << *iter << endl;
298  }
299 
300  // Add some additional necessary lines to the Herwig input config
301  herwiginputconfig << "saverun " << run_ << " " << generator_ << endl;
302  // write the ProxyID for the RandomEngineGlue to fill its pointer in
303  ostringstream ss;
304  ss << randomEngineGlueProxy_->getID();
305  //herwiginputconfig << "set " << generator_ << ":RandomNumberGenerator:ProxyID " << ss.str() << endl;
306 
307  // Dump Herwig input config to file, so that it can be read by Herwig
308  cfgDump << herwiginputconfig.str() << endl;
309  cfgDump.close();
310 }
static double pthat(const ThePEG::EventPtr &event)
void initRepository(const edm::ParameterSet &params)
void createInputFile(const edm::ParameterSet &params)
def create(alignables, pedeDump, additionalData, outputFile, config)
std::shared_ptr< Herwig::HerwigUIProvider > HwUI_
void setPEGRandomEngine(CLHEP::HepRandomEngine *)
const_iterator begin() const
int init
Definition: HydjetWrapper.h:66
CLHEP::HepRandomEngine * randomEngine
std::unique_ptr< HepMC::IO_BaseClass > iobc_
const_iterator end() const
~Herwig7Interface() noexcept
std::string dumpConfig_
Log< level::Error, false > LogError
ThePEG::EGPtr eg_
std::shared_ptr< ThePEG::RandomEngineGlue::Proxy > randomEngineGlueProxy_
static std::unique_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
const std::string run_
def convert(infile, ofile)
Herwig7Interface(const edm::ParameterSet &params)
const std::string generator_
Log< level::Info, false > LogInfo
const unsigned int skipEvents_
void flushRandomNumberGenerator()
void setRandomEngine(CLHEP::HepRandomEngine *v)
Log< level::Warning, false > LogWarning
tmp
align.sh
Definition: createJobs.py:716
Definition: event.py:1
static const int ERROR
unsigned transform(const HcalDetId &id, unsigned transformCode)