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