CMS 3D CMS Logo

Herwig7Interface.cc
Go to the documentation of this file.
1 
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 #include <cstdlib>
11 #include <memory>
12 #include <cmath>
13 #include <cstdlib>
14 
15 #include <algorithm>
16 
17 #include <HepMC/GenEvent.h>
18 #include <HepMC/PdfInfo.h>
19 #include <HepMC/IO_GenEvent.h>
20 
21 #include <Herwig/API/HerwigAPI.h>
22 
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>
36 
38 
40 
44 
45 #include "CLHEP/Random/RandomEngine.h"
46 
47 using namespace std;
48 using namespace gen;
49 
51  : randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
52  dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
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)) {
57  // Write events in hepmc ascii format for debugging purposes
58  string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
59  if (!dumpEvents.empty()) {
60  iobc_.reset(new HepMC::IO_GenEvent(dumpEvents, ios::out));
61  edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
62  }
63  // Clear dumpConfig target
64  if (!dumpConfig_.empty())
65  ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
66 }
67 
69  if (eg_)
70  eg_->finalize();
71  edm::LogInfo("Herwig7Interface") << "Event generator finalized";
72 }
73 
74 void Herwig7Interface::setPEGRandomEngine(CLHEP::HepRandomEngine *v) {
75  randomEngineGlueProxy_->setRandomEngine(v);
76  randomEngine = v;
78  if (rnd) {
79  rnd->setRandomEngine(v);
80  }
81 }
82 
84  std::string runModeTemp = pset.getUntrackedParameter<string>("runModeList", "read,run");
85 
86  // To Lower
87  std::transform(runModeTemp.begin(), runModeTemp.end(), runModeTemp.begin(), ::tolower);
88 
89  while (!runModeTemp.empty()) {
90  // Split first part of List
91  std::string choice;
92  size_t pos = runModeTemp.find(',');
93  if (std::string::npos == pos)
94  choice = runModeTemp;
95  else
96  choice = runModeTemp.substr(0, pos);
97 
98  if (pos == std::string::npos)
99  runModeTemp.erase();
100  else
101  runModeTemp.erase(0, pos + 1);
102 
103  HwUI_.reset(new Herwig::HerwigUIProvider(pset, dumpConfig_, Herwig::RunMode::READ));
104  edm::LogInfo("Herwig7Interface") << "HerwigUIProvider object with run mode " << HwUI_->runMode() << " created.\n";
105 
106  // Chose run mode
107  if (choice == "read") {
109  HwUI_->setRunMode(Herwig::RunMode::READ, pset, dumpConfig_);
110  edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
111  << " will be passed to Herwig for the read step.\n";
113  } else if (choice == "build") {
115  HwUI_->setRunMode(Herwig::RunMode::BUILD, pset, dumpConfig_);
116  edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
117  << " will be passed to Herwig for the build step.\n";
119 
120  } else if (choice == "integrate") {
121  std::string runFileName = run_ + ".run";
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);
126 
127  } else if (choice == "run") {
128  std::string runFileName = run_ + ".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);
131  } else {
132  edm::LogInfo("Herwig7Interface") << "Cannot recognize \"" << choice << "\".\n"
133  << "Trying to skip step.\n";
134  continue;
135  }
136  }
137 }
138 
140  try {
141  edm::LogInfo("Herwig7Interface") << "callHerwigGenerator function invoked with run mode " << HwUI_->runMode()
142  << ".\n";
143 
144  // Call program switches according to runMode
145  switch (HwUI_->runMode()) {
146  case Herwig::RunMode::INIT:
148  break;
149  case Herwig::RunMode::READ:
151  break;
152  case Herwig::RunMode::BUILD:
154  break;
155  case Herwig::RunMode::INTEGRATE:
156  Herwig::API::integrate(*HwUI_);
157  break;
158  case Herwig::RunMode::MERGEGRIDS:
159  Herwig::API::mergegrids(*HwUI_);
160  break;
161  case Herwig::RunMode::RUN: {
162  HwUI_->setSeed(randomEngine->getSeed());
163  eg_ = Herwig::API::prepareRun(*HwUI_);
164  break;
165  }
167  edm::LogError("Herwig7Interface") << "Error during read in of command line parameters.\n"
168  << "Program execution will stop now.";
169  return;
170  default:
171  HwUI_->quitWithHelp();
172  }
173 
174  return;
175 
176  } catch (ThePEG::Exception &e) {
177  edm::LogError("Herwig7Interface") << ": ThePEG::Exception caught.\n"
178  << e.what() << '\n'
179  << "See logfile for details.\n";
180  return;
181  } catch (std::exception &e) {
182  edm::LogError("Herwig7Interface") << ": " << e.what() << '\n';
183  return;
184  } catch (const char *what) {
185  edm::LogError("Herwig7Interface") << ": caught exception: " << what << "\n";
186  return;
187  }
188 }
189 
191  if (HwUI_->runMode() == Herwig::RunMode::RUN) {
192  edm::LogInfo("Herwig7Interface") << "Starting EventGenerator initialization";
194  edm::LogInfo("Herwig7Interface") << "EventGenerator initialized";
195 
196  // Skip events
197  for (unsigned int i = 0; i < skipEvents_; i++) {
199  eg_->shoot();
200  edm::LogInfo("Herwig7Interface") << "Event discarded";
201  }
202 
203  return true;
204 
205  } else {
206  edm::LogInfo("Herwig7Interface") << "Stopped EventGenerator due to missing run mode.";
207  return false;
208  /*
209  throw cms::Exception("Herwig7Interface")
210  << "EventGenerator could not be initialized due to wrong run mode!" << endl;
211 */
212  }
213 }
214 
216  /*ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
217 
218  if (!rnd)
219  edm::LogWarning("ProxyMissing")
220  << "ThePEG not initialised with RandomEngineGlue.";
221  else
222  rnd->flush();
223  */
224 }
225 
226 auto_ptr<HepMC::GenEvent> Herwig7Interface::convert(const ThePEG::EventPtr &event) {
227  return std::auto_ptr<HepMC::GenEvent>(ThePEG::HepMCConverter<HepMC::GenEvent>::convert(*event));
228 }
229 
230 double Herwig7Interface::pthat(const ThePEG::EventPtr &event) {
231  using namespace ThePEG;
232 
233  if (!event->primaryCollision())
234  return -1.0;
235 
236  tSubProPtr sub = event->primaryCollision()->primarySubProcess();
237  TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(sub->incoming()));
238 
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);
242 
243  return pthat;
244 }
245 
247  /* Initialize the input config for Herwig from
248  * 1. the Herwig7 config files
249  * 2. the CMSSW config blocks
250  * Writes them to an output file which is read by Herwig
251  */
252 
253  stringstream logstream;
254 
255  // Contains input config passed to Herwig
256  stringstream herwiginputconfig;
257 
258  // Define output file to which input config is written, too, if dumpConfig parameter is set.
259  // Otherwise use default file HerwigConfig.in which is read in by Herwig
260  ofstream cfgDump;
261  cfgDump.open(dumpConfig_.c_str(), ios_base::app);
262 
263  // Read Herwig config files as input
264  vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
265  // Loop over the config files
266  for (const auto &iter : configFiles) {
267  // Open external config file
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();
274 
275  // Comment out occurence of saverun in config file since it is set later considering run and generator option
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
279  << ")" << endl;
280  configFileContent.insert(configFileContent.find(searchKeyword), "#");
281  }
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;
286  } else {
287  edm::LogWarning("Herwig7Interface") << "Could not read config file (" << iter << ")" << endl;
288  }
289  }
290 
291  edm::LogInfo("Herwig7Interface") << "Start with processing CMSSW config" << endl;
292  // Read CMSSW config file parameter sets starting from "parameterSets"
293  ParameterCollector collector(pset);
295  iter = collector.begin();
296  herwiginputconfig << endl << "# Begin Parameter set input\n" << endl;
297  for (; iter != collector.end(); ++iter) {
298  herwiginputconfig << *iter << endl;
299  }
300 
301  // Add some additional necessary lines to the Herwig input config
302  herwiginputconfig << "saverun " << run_ << " " << generator_ << endl;
303  // write the ProxyID for the RandomEngineGlue to fill its pointer in
304  ostringstream ss;
305  ss << randomEngineGlueProxy_->getID();
306  //herwiginputconfig << "set " << generator_ << ":RandomNumberGenerator:ProxyID " << ss.str() << endl;
307 
308  // Dump Herwig input config to file, so that it can be read by Herwig
309  cfgDump << herwiginputconfig.str() << endl;
310  cfgDump.close();
311 }
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 *)
std::auto_ptr< HepMC::IO_BaseClass > iobc_
const_iterator begin() const
int init
Definition: HydjetWrapper.h:64
CLHEP::HepRandomEngine * randomEngine
const_iterator end() const
~Herwig7Interface() noexcept
std::string dumpConfig_
Log< level::Error, false > LogError
ThePEG::EGPtr eg_
std::shared_ptr< ThePEG::RandomEngineGlue::Proxy > randomEngineGlueProxy_
const std::string run_
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
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)