MEMAEV

MEMAIN

OUTTREE

PYPART

Param

UPPRIV

Macros

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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
#include <memory>
#include <cstring>
#include <string>
#include <cctype>
#include <map>
#include <set>

#include <boost/algorithm/string/trim.hpp>

// #include <HepMC/GenEvent.h>

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

#include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
#include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
#include "GeneratorInterface/PartonShowerVeto/interface/JetMatchingMadgraph.h"

namespace gen {

  extern "C" {
#define PARAMLEN 20
  namespace {
    struct Param {
      Param(const std::string &str) {
        int len = std::min(PARAMLEN, (int)str.length());
        std::memcpy(value, str.c_str(), len);
        std::memset(value + len, ' ', PARAMLEN - len);
      }

      char value[PARAMLEN];
    };
  }  // namespace
  extern void mginit_(int *npara, Param *params, Param *values);
  extern void mgevnt_(void);
  extern void mgveto_(int *veto);

  extern struct UPPRIV {
    int lnhin, lnhout;
    int mscal, ievnt;
    int ickkw, iscale;
  } uppriv_;

  extern struct MEMAIN {
    double etcjet, rclmax, etaclmax, qcut, showerkt, clfact;
    int maxjets, minjets, iexcfile, ktsche;
    int mektsc, nexcres, excres[30];
    int nqmatch, excproc, iexcproc[1000], iexcval[1000];
    bool nosingrad, jetprocs;
  } memain_;

  extern struct OUTTREE {
    int flag;
  } outtree_;

  extern struct MEMAEV {
    double ptclus[20];
    int nljets, iexc, ifile;
  } memaev_;

  extern struct PYPART {
    int npart, npartd, ipart[1000];
    double ptpart[1000];
  } pypart_;
  }  // extern "C"

  template <typename T>
  T JetMatchingMadgraph::parseParameter(const std::string &value) {
    std::istringstream ss(value);
    T result;
    ss >> result;
    return result;
  }

  template <>
  std::string JetMatchingMadgraph::parseParameter(const std::string &value) {
    std::string result;
    if (!result.empty() && result[0] == '\'')
      result = result.substr(1);
    if (!result.empty() && result[result.length() - 1] == '\'')
      result.resize(result.length() - 1);
    return result;
  }

  template <>
  bool JetMatchingMadgraph::parseParameter(const std::string &value_) {
    std::string value(value_);
    std::transform(value.begin(), value.end(), value.begin(), (int (*)(int))std::toupper);
    return value == "T" || value == "Y" || value == "True" || value == "1" || value == ".TRUE.";
  }

  template <typename T>
  T JetMatchingMadgraph::getParameter(const std::map<std::string, std::string> &params,
                                      const std::string &var,
                                      const T &defValue) {
    std::map<std::string, std::string>::const_iterator pos = params.find(var);
    if (pos == params.end())
      return defValue;
    return parseParameter<T>(pos->second);
  }

  template <typename T>
  T JetMatchingMadgraph::getParameter(const std::string &var, const T &defValue) const {
    return getParameter(mgParams, var, defValue);
  }

  JetMatchingMadgraph::JetMatchingMadgraph(const edm::ParameterSet &params)
      : JetMatching(params), runInitialized(false) {
    std::string mode = params.getParameter<std::string>("mode");
    if (mode == "inclusive") {
      soup = false;
      exclusive = false;
    } else if (mode == "exclusive") {
      soup = false;
      exclusive = true;
    } else if (mode == "auto")
      soup = true;
    else
      throw cms::Exception("Generator|LHEInterface") << "Madgraph jet matching scheme requires \"mode\" "
                                                        "parameter to be set to either \"inclusive\", "
                                                        "\"exclusive\" or \"auto\"."
                                                     << std::endl;

    memain_.etcjet = 0.;
    memain_.rclmax = 0.0;
    memain_.clfact = 0.0;
    memain_.ktsche = 0.0;
    memain_.etaclmax = params.getParameter<double>("MEMAIN_etaclmax");
    memain_.qcut = params.getParameter<double>("MEMAIN_qcut");
    memain_.minjets = params.getParameter<int>("MEMAIN_minjets");
    memain_.maxjets = params.getParameter<int>("MEMAIN_maxjets");
    memain_.showerkt = params.getParameter<double>("MEMAIN_showerkt");
    memain_.nqmatch = params.getParameter<int>("MEMAIN_nqmatch");
    outtree_.flag = params.getParameter<int>("outTree_flag");
    std::string list_excres = params.getParameter<std::string>("MEMAIN_excres");
    std::vector<std::string> elems;
    std::stringstream ss(list_excres);
    std::string item;
    int index = 0;
    while (std::getline(ss, item, ',')) {
      elems.push_back(item);
      memain_.excres[index] = std::atoi(item.c_str());
      index++;
    }
    memain_.nexcres = index;
  }

  JetMatchingMadgraph::~JetMatchingMadgraph() {}

  double JetMatchingMadgraph::getJetEtaMax() const { return memain_.etaclmax; }

  std::set<std::string> JetMatchingMadgraph::capabilities() const {
    std::set<std::string> result;
    result.insert("psFinalState");
    result.insert("hepevt");
    result.insert("pythia6");
    return result;
  }

  static std::map<std::string, std::string> parseHeader(const std::vector<std::string> &header) {
    std::map<std::string, std::string> params;

    for (std::vector<std::string>::const_iterator iter = header.begin(); iter != header.end(); ++iter) {
      std::string line = *iter;
      if (line.empty() || line[0] == '#')
        continue;

      std::string::size_type pos = line.find('!');
      if (pos != std::string::npos)
        line.resize(pos);

      pos = line.find('=');
      if (pos == std::string::npos)
        continue;

      std::string var = boost::algorithm::trim_copy(line.substr(pos + 1));
      std::string value = boost::algorithm::trim_copy(line.substr(0, pos));

      params[var] = value;
    }

    return params;
  }

  template <typename T>
  void JetMatchingMadgraph::updateOrDie(const std::map<std::string, std::string> &params,
                                        T &param,
                                        const std::string &name) {
    if (param < 0) {
      param = getParameter(params, name, param);
    }
    if (param < 0)
      throw cms::Exception("Generator|PartonShowerVeto") << "The MGParamCMS header does not specify the jet "
                                                            "matching parameter \""
                                                         << name
                                                         << "\", but it "
                                                            "is requested by the CMSSW configuration."
                                                         << std::endl;
  }

  // implements the Madgraph method - use ME2pythia.f

  void JetMatchingMadgraph::init(const lhef::LHERunInfo *runInfo) {
    // read MadGraph run card

    std::map<std::string, std::string> parameters;

    std::vector<std::string> header = runInfo->findHeader("MGRunCard");
    if (header.empty())
      throw cms::Exception("Generator|PartonShowerVeto") << "In order to use MadGraph jet matching, "
                                                            "the input file has to contain the corresponding "
                                                            "MadGraph headers."
                                                         << std::endl;

    mgParams = parseHeader(header);

    // set variables in common block

    std::vector<Param> params;
    std::vector<Param> values;
    for (std::map<std::string, std::string>::const_iterator iter = mgParams.begin(); iter != mgParams.end(); ++iter) {
      params.push_back(" " + iter->first);
      values.push_back(iter->second);
    }

    // set MG matching parameters

    uppriv_.ickkw = getParameter<int>("ickkw", 0);
    memain_.mektsc = getParameter<int>("ktscheme", 0);

    header = runInfo->findHeader("MGParamCMS");

    std::map<std::string, std::string> mgInfoCMS = parseHeader(header);

    for (std::map<std::string, std::string>::const_iterator iter = mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
      std::cout << "mgInfoCMS: " << iter->first << " " << iter->second << std::endl;
    }

    updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
    updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
    updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
    updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
    updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
    updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");

    // run Fortran initialization code

    int nparam = params.size();
    mginit_(&nparam, &params.front(), &values.front());
    runInitialized = true;
  }

  //void JetMatchingMadgraph::beforeHadronisation(
  //				const std::shared_ptr<lhef::LHEEvent> &event)

  void JetMatchingMadgraph::beforeHadronisation(const lhef::LHEEvent *event) {
    if (!runInitialized)
      throw cms::Exception("Generator|PartonShowerVeto") << "Run not initialized in JetMatchingMadgraph" << std::endl;

    if (uppriv_.ickkw) {
      std::vector<std::string> comments = event->getComments();
      if (comments.size() == 1) {
        std::istringstream ss(comments[0].substr(1));
        for (int i = 0; i < 1000; i++) {
          double pt;
          ss >> pt;
          if (!ss.good())
            break;
          pypart_.ptpart[i] = pt;
        }
      } else {
        edm::LogWarning("Generator|LHEInterface") << "Expected exactly one comment line per "
                                                     "event containing MadGraph parton scale "
                                                     "information."
                                                  << std::endl;

        const lhef::HEPEUP *hepeup = event->getHEPEUP();
        for (int i = 2; i < hepeup->NUP; i++) {
          double mt2 = hepeup->PUP[i][0] * hepeup->PUP[i][0] + hepeup->PUP[i][1] * hepeup->PUP[i][1] +
                       hepeup->PUP[i][4] * hepeup->PUP[i][4];
          pypart_.ptpart[i - 2] = std::sqrt(mt2);
        }
      }
    }

    // mgevnt_();
    eventInitialized = true;
  }

  void JetMatchingMadgraph::beforeHadronisationExec() {
    mgevnt_();
    eventInitialized = true;
    return;
  }

  /*
int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
                                  const HepMC::GenEvent *finalState,
                                  bool showeredFinalState)
*/
  int JetMatchingMadgraph::match(const lhef::LHEEvent *partonLevel, const std::vector<fastjet::PseudoJet> *jetInput) {
    /*
	if (!showeredFinalState)
		throw cms::Exception("Generator|LHEInterface")
			<< "MadGraph matching expected parton shower "
			   "final state." << std::endl;
*/

    if (!runInitialized)
      throw cms::Exception("Generator|LHEInterface") << "Run not initialized in JetMatchingMadgraph" << std::endl;

    if (!eventInitialized)
      throw cms::Exception("Generator|LHEInterface") << "Event not initialized in JetMatchingMadgraph" << std::endl;

    if (soup)
      memaev_.iexc = (memaev_.nljets < memain_.maxjets);
    else
      memaev_.iexc = exclusive;

    int veto = 0;
    mgveto_(&veto);
    fMatchingStatus = true;
    eventInitialized = false;

    return veto;
  }

}  // end namespace gen