Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
/** \class Herwig7Interface
 *  
 *  Marco A. Harrendorf marco.harrendorf@cern.ch
 *  Dominik Beutel dominik.beutel@cern.ch
 */

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>

#include <algorithm>

#include <HepMC/GenEvent.h>
#include <HepMC/PdfInfo.h>
#include <HepMC/IO_GenEvent.h>

#include <Herwig/API/HerwigAPI.h>

#include <ThePEG/Utilities/DynamicLoader.h>
#include <ThePEG/Repository/Repository.h>
#include <ThePEG/Handlers/EventHandler.h>
#include <ThePEG/Handlers/XComb.h>
#include <ThePEG/EventRecord/Event.h>
#include <ThePEG/EventRecord/Particle.h>
#include <ThePEG/EventRecord/Collision.h>
#include <ThePEG/EventRecord/TmpTransform.h>
#include <ThePEG/Config/ThePEG.h>
#include <ThePEG/PDF/PartonExtractor.h>
#include <ThePEG/PDF/PDFBase.h>
#include <ThePEG/Utilities/UtilityBase.h>
#include <ThePEG/Vectors/HepMCConverter.h>

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "GeneratorInterface/Core/interface/ParameterCollector.h"

#include "GeneratorInterface/Herwig7Interface/interface/Proxy.h"
#include "GeneratorInterface/Herwig7Interface/interface/RandomEngineGlue.h"
#include "GeneratorInterface/Herwig7Interface/interface/Herwig7Interface.h"

#include "CLHEP/Random/RandomEngine.h"

using namespace std;
using namespace gen;

Herwig7Interface::Herwig7Interface(const edm::ParameterSet &pset)
    : randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::create()),
      dataLocation_(ParameterCollector::resolve(pset.getParameter<string>("dataLocation"))),
      generator_(pset.getParameter<string>("generatorModule")),
      run_(pset.getParameter<string>("run")),
      dumpConfig_(pset.getUntrackedParameter<string>("dumpConfig", "HerwigConfig.in")),
      skipEvents_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0)) {
  // Write events in hepmc ascii format for debugging purposes
  string dumpEvents = pset.getUntrackedParameter<string>("dumpEvents", "");
  if (!dumpEvents.empty()) {
    iobc_ = std::make_unique<HepMC::IO_GenEvent>(dumpEvents, ios::out);
    edm::LogInfo("ThePEGSource") << "Event logging switched on (=> " << dumpEvents << ")";
  }
  // Clear dumpConfig target
  if (!dumpConfig_.empty())
    ofstream cfgDump(dumpConfig_.c_str(), ios_base::trunc);
}

Herwig7Interface::~Herwig7Interface() noexcept {
  if (eg_)
    eg_->finalize();
  edm::LogInfo("Herwig7Interface") << "Event generator finalized";
}

void Herwig7Interface::setPEGRandomEngine(CLHEP::HepRandomEngine *v) {
  randomEngineGlueProxy_->setRandomEngine(v);
  randomEngine = v;
  ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();
  if (rnd) {
    rnd->setRandomEngine(v);
  }
}

void Herwig7Interface::initRepository(const edm::ParameterSet &pset) {
  std::string runModeTemp = pset.getUntrackedParameter<string>("runModeList", "read,run");

  // To Lower
  std::transform(runModeTemp.begin(), runModeTemp.end(), runModeTemp.begin(), ::tolower);

  while (!runModeTemp.empty()) {
    // Split first part of List
    std::string choice;
    size_t pos = runModeTemp.find(',');
    if (std::string::npos == pos)
      choice = runModeTemp;
    else
      choice = runModeTemp.substr(0, pos);

    if (pos == std::string::npos)
      runModeTemp.erase();
    else
      runModeTemp.erase(0, pos + 1);

    HwUI_ = std::make_shared<Herwig::HerwigUIProvider>(pset, dumpConfig_, Herwig::RunMode::READ);
    edm::LogInfo("Herwig7Interface") << "HerwigUIProvider object with run mode " << HwUI_->runMode() << " created.\n";

    // Chose run mode
    if (choice == "read") {
      createInputFile(pset);
      HwUI_->setRunMode(Herwig::RunMode::READ, pset, dumpConfig_);
      edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
                                       << " will be passed to Herwig for the read step.\n";
      callHerwigGenerator();
    } else if (choice == "build") {
      createInputFile(pset);
      HwUI_->setRunMode(Herwig::RunMode::BUILD, pset, dumpConfig_);
      edm::LogInfo("Herwig7Interface") << "Input file " << dumpConfig_
                                       << " will be passed to Herwig for the build step.\n";
      callHerwigGenerator();

    } else if (choice == "integrate") {
      std::string runFileName = run_ + ".run";
      edm::LogInfo("Herwig7Interface") << "Run file " << runFileName
                                       << " will be passed to Herwig for the integrate step.\n";
      HwUI_->setRunMode(Herwig::RunMode::INTEGRATE, pset, runFileName);
      callHerwigGenerator();

    } else if (choice == "run") {
      std::string runFileName = run_ + ".run";
      edm::LogInfo("Herwig7Interface") << "Run file " << runFileName << " will be passed to Herwig for the run step.\n";
      HwUI_->setRunMode(Herwig::RunMode::RUN, pset, runFileName);
    } else {
      edm::LogInfo("Herwig7Interface") << "Cannot recognize \"" << choice << "\".\n"
                                       << "Trying to skip step.\n";
      continue;
    }
  }
}

void Herwig7Interface::callHerwigGenerator() {
  try {
    edm::LogInfo("Herwig7Interface") << "callHerwigGenerator function invoked with run mode " << HwUI_->runMode()
                                     << ".\n";

    // Call program switches according to runMode
    switch (HwUI_->runMode()) {
      case Herwig::RunMode::INIT:
        Herwig::API::init(*HwUI_);
        break;
      case Herwig::RunMode::READ:
        Herwig::API::read(*HwUI_);
        break;
      case Herwig::RunMode::BUILD:
        Herwig::API::build(*HwUI_);
        break;
      case Herwig::RunMode::INTEGRATE:
        Herwig::API::integrate(*HwUI_);
        break;
      case Herwig::RunMode::MERGEGRIDS:
        Herwig::API::mergegrids(*HwUI_);
        break;
      case Herwig::RunMode::RUN: {
        HwUI_->setSeed(randomEngine->getSeed());
        eg_ = Herwig::API::prepareRun(*HwUI_);
        break;
      }
      case Herwig::RunMode::ERROR:
        edm::LogError("Herwig7Interface") << "Error during read in of command line parameters.\n"
                                          << "Program execution will stop now.";
        return;
      default:
        HwUI_->quitWithHelp();
    }

    return;

  } catch (ThePEG::Exception &e) {
    edm::LogError("Herwig7Interface") << ": ThePEG::Exception caught.\n"
                                      << e.what() << '\n'
                                      << "See logfile for details.\n";
    return;
  } catch (std::exception &e) {
    edm::LogError("Herwig7Interface") << ": " << e.what() << '\n';
    return;
  } catch (const char *what) {
    edm::LogError("Herwig7Interface") << ": caught exception: " << what << "\n";
    return;
  }
}

bool Herwig7Interface::initGenerator() {
  if (HwUI_->runMode() == Herwig::RunMode::RUN) {
    edm::LogInfo("Herwig7Interface") << "Starting EventGenerator initialization";
    callHerwigGenerator();
    edm::LogInfo("Herwig7Interface") << "EventGenerator initialized";

    // Skip events
    for (unsigned int i = 0; i < skipEvents_; i++) {
      flushRandomNumberGenerator();
      eg_->shoot();
      edm::LogInfo("Herwig7Interface") << "Event discarded";
    }

    return true;

  } else {
    edm::LogInfo("Herwig7Interface") << "Stopped EventGenerator due to missing run mode.";
    return false;
    /*
		throw cms::Exception("Herwig7Interface")
			<< "EventGenerator could not be initialized due to wrong run mode!" << endl;
*/
  }
}

void Herwig7Interface::flushRandomNumberGenerator() {
  /*ThePEG::RandomEngineGlue *rnd = randomEngineGlueProxy_->getInstance();

	if (!rnd)
		edm::LogWarning("ProxyMissing")
			<< "ThePEG not initialised with RandomEngineGlue.";
	else
		rnd->flush();
      */
}

unique_ptr<HepMC::GenEvent> Herwig7Interface::convert(const ThePEG::EventPtr &event) {
  return std::unique_ptr<HepMC::GenEvent>(ThePEG::HepMCConverter<HepMC::GenEvent>::convert(*event));
}

double Herwig7Interface::pthat(const ThePEG::EventPtr &event) {
  using namespace ThePEG;

  if (!event->primaryCollision())
    return -1.0;

  tSubProPtr sub = event->primaryCollision()->primarySubProcess();
  TmpTransform<tSubProPtr> tmp(sub, Utilities::getBoostToCM(sub->incoming()));

  double pthat = (*sub->outgoing().begin())->momentum().perp() / ThePEG::GeV;
  for (PVector::const_iterator it = sub->outgoing().begin(); it != sub->outgoing().end(); ++it)
    pthat = std::min<double>(pthat, (*it)->momentum().perp() / ThePEG::GeV);

  return pthat;
}

void Herwig7Interface::createInputFile(const edm::ParameterSet &pset) {
  /* Initialize the input config for Herwig from
	 * 1. the Herwig7 config files
	 * 2. the CMSSW config blocks
	 * Writes them to an output file which is read by Herwig
	*/

  stringstream logstream;

  // Contains input config passed to Herwig
  stringstream herwiginputconfig;

  // Define output file to which input config is written, too, if dumpConfig parameter is set.
  // Otherwise use default file HerwigConfig.in which is read in by Herwig
  ofstream cfgDump;
  cfgDump.open(dumpConfig_.c_str(), ios_base::app);

  // Read Herwig config files as input
  vector<string> configFiles = pset.getParameter<vector<string> >("configFiles");
  // Loop over the config files
  for (const auto &iter : configFiles) {
    // Open external config file
    ifstream externalConfigFile(iter);
    if (externalConfigFile.is_open()) {
      edm::LogInfo("Herwig7Interface") << "Reading config file (" << iter << ")" << endl;
      stringstream configFileStream;
      configFileStream << externalConfigFile.rdbuf();
      string configFileContent = configFileStream.str();

      // Comment out occurence of saverun in config file since it is set later considering run and generator option
      string searchKeyword("saverun");
      if (configFileContent.find(searchKeyword) != std::string::npos) {
        edm::LogInfo("Herwig7Interface") << "Commented out saverun command in external input config file(" << iter
                                         << ")" << endl;
        configFileContent.insert(configFileContent.find(searchKeyword), "#");
      }
      herwiginputconfig << "# Begin Config file input" << endl
                        << configFileContent << endl
                        << "# End Config file input";
      edm::LogInfo("Herwig7Interface") << "Finished reading config file (" << iter << ")" << endl;
    } else {
      edm::LogWarning("Herwig7Interface") << "Could not read config file (" << iter << ")" << endl;
    }
  }

  edm::LogInfo("Herwig7Interface") << "Start with processing CMSSW config" << endl;
  // Read CMSSW config file parameter sets starting from "parameterSets"
  ParameterCollector collector(pset);
  ParameterCollector::const_iterator iter;
  iter = collector.begin();
  herwiginputconfig << endl << "# Begin Parameter set input\n" << endl;
  for (; iter != collector.end(); ++iter) {
    herwiginputconfig << *iter << endl;
  }

  // Add some additional necessary lines to the Herwig input config
  herwiginputconfig << "saverun " << run_ << " " << generator_ << endl;
  // write the ProxyID for the RandomEngineGlue to fill its pointer in
  ostringstream ss;
  ss << randomEngineGlueProxy_->getID();
  //herwiginputconfig << "set " << generator_ << ":RandomNumberGenerator:ProxyID " << ss.str() << endl;

  // Dump Herwig input config to file, so that it can be read by Herwig
  cfgDump << herwiginputconfig.str() << endl;
  cfgDump.close();
}