00001 #include <functional>
00002 #include <algorithm>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <vector>
00006 #include <memory>
00007 #include <cstring>
00008 #include <string>
00009 #include <cctype>
00010 #include <map>
00011 #include <set>
00012
00013 #include <boost/shared_ptr.hpp>
00014 #include <boost/algorithm/string/trim.hpp>
00015
00016
00017
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020
00021 #include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
00022 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00023 #include "GeneratorInterface/PartonShowerVeto/interface/JetMatchingMadgraph.h"
00024
00025 namespace gen {
00026
00027 extern "C" {
00028 #define PARAMLEN 20
00029 namespace {
00030 struct Param {
00031 Param(const std::string &str)
00032 {
00033 int len = std::min(PARAMLEN,
00034 (int)str.length());
00035 std::memcpy(value, str.c_str(), len);
00036 std::memset(value + len, ' ', PARAMLEN - len);
00037 }
00038
00039 char value[PARAMLEN];
00040 };
00041 }
00042 extern void mginit_(int *npara, Param *params, Param *values);
00043 extern void mgevnt_(void);
00044 extern void mgveto_(int *veto);
00045
00046 extern struct UPPRIV {
00047 int lnhin, lnhout;
00048 int mscal, ievnt;
00049 int ickkw, iscale;
00050 } uppriv_;
00051
00052 extern struct MEMAIN {
00053 double etcjet, rclmax, etaclmax, qcut, showerkt, clfact;
00054 int maxjets, minjets, iexcfile, ktsche;
00055 int mektsc,nexcres, excres[30];
00056 int nqmatch,excproc,iexcproc[1000],iexcval[1000];
00057 bool nosingrad,jetprocs;
00058 } memain_;
00059
00060 extern struct OUTTREE {
00061 int flag;
00062 } outtree_;
00063
00064 extern struct MEMAEV {
00065 double ptclus[20];
00066 int nljets, iexc, ifile;
00067 } memaev_;
00068
00069 extern struct PYPART {
00070 int npart, npartd, ipart[1000];
00071 double ptpart[1000];
00072 } pypart_;
00073 }
00074
00075
00076 template<typename T>
00077 T JetMatchingMadgraph::parseParameter(const std::string &value)
00078 {
00079 std::istringstream ss(value);
00080 T result;
00081 ss >> result;
00082 return result;
00083 }
00084
00085 template<>
00086 std::string JetMatchingMadgraph::parseParameter(const std::string &value)
00087 {
00088 std::string result;
00089 if (!result.empty() && result[0] == '\'')
00090 result = result.substr(1);
00091 if (!result.empty() && result[result.length() - 1] == '\'')
00092 result.resize(result.length() - 1);
00093 return result;
00094 }
00095
00096 template<>
00097 bool JetMatchingMadgraph::parseParameter(const std::string &value_)
00098 {
00099 std::string value(value_);
00100 std::transform(value.begin(), value.end(),
00101 value.begin(), (int(*)(int))std::toupper);
00102 return value == "T" || value == "Y" || value=="True" ||
00103 value == "1" || value == ".TRUE.";
00104 }
00105
00106 template<typename T>
00107 T JetMatchingMadgraph::getParameter(
00108 const std::map<std::string, std::string> ¶ms,
00109 const std::string &var, const T &defValue)
00110 {
00111 std::map<std::string, std::string>::const_iterator pos =
00112 params.find(var);
00113 if (pos == params.end())
00114 return defValue;
00115 return parseParameter<T>(pos->second);
00116 }
00117
00118 template<typename T>
00119 T JetMatchingMadgraph::getParameter(const std::string &var,
00120 const T &defValue) const
00121 {
00122 return getParameter(mgParams, var, defValue);
00123 }
00124
00125 JetMatchingMadgraph::JetMatchingMadgraph(const edm::ParameterSet ¶ms) :
00126 JetMatching(params),
00127 runInitialized(false)
00128 {
00129 std::string mode = params.getParameter<std::string>("mode");
00130 if (mode == "inclusive") {
00131 soup = false;
00132 exclusive = false;
00133 } else if (mode == "exclusive") {
00134 soup = false;
00135 exclusive = true;
00136 } else if (mode == "auto")
00137 soup = true;
00138 else
00139 throw cms::Exception("Generator|LHEInterface")
00140 << "Madgraph jet matching scheme requires \"mode\" "
00141 "parameter to be set to either \"inclusive\", "
00142 "\"exclusive\" or \"auto\"." << std::endl;
00143
00144 memain_.etcjet = 0.;
00145 memain_.rclmax = 0.0;
00146 memain_.clfact = 0.0;
00147 memain_.ktsche = 0.0;
00148 memain_.etaclmax = params.getParameter<double>("MEMAIN_etaclmax");
00149 memain_.qcut = params.getParameter<double>("MEMAIN_qcut");
00150 memain_.minjets = params.getParameter<int>("MEMAIN_minjets");
00151 memain_.maxjets = params.getParameter<int>("MEMAIN_maxjets");
00152 memain_.showerkt = params.getParameter<double>("MEMAIN_showerkt");
00153 memain_.nqmatch = params.getParameter<int>("MEMAIN_nqmatch");
00154 outtree_.flag = params.getParameter<int>("outTree_flag");
00155 std::string list_excres = params.getParameter<std::string>("MEMAIN_excres");
00156 std::vector<std::string> elems;
00157 std::stringstream ss(list_excres);
00158 std::string item;
00159 int index=0;
00160 while(std::getline(ss, item, ',')) {
00161 elems.push_back(item);
00162 memain_.excres[index]=std::atoi(item.c_str());
00163 index++;
00164 }
00165 memain_.nexcres=index;
00166
00167
00168
00169 }
00170
00171 JetMatchingMadgraph::~JetMatchingMadgraph()
00172 {
00173 }
00174
00175 double JetMatchingMadgraph::getJetEtaMax() const
00176 {
00177 return memain_.etaclmax;
00178 }
00179
00180 std::set<std::string> JetMatchingMadgraph::capabilities() const
00181 {
00182 std::set<std::string> result;
00183 result.insert("psFinalState");
00184 result.insert("hepevt");
00185 result.insert("pythia6");
00186 return result;
00187 }
00188
00189 static std::map<std::string, std::string>
00190 parseHeader(const std::vector<std::string> &header)
00191 {
00192 std::map<std::string, std::string> params;
00193
00194 for(std::vector<std::string>::const_iterator iter = header.begin();
00195 iter != header.end(); ++iter) {
00196 std::string line = *iter;
00197 if (line.empty() || line[0] == '#')
00198 continue;
00199
00200 std::string::size_type pos = line.find('!');
00201 if (pos != std::string::npos)
00202 line.resize(pos);
00203
00204 pos = line.find('=');
00205 if (pos == std::string::npos)
00206 continue;
00207
00208 std::string var =
00209 boost::algorithm::trim_copy(line.substr(pos + 1));
00210 std::string value =
00211 boost::algorithm::trim_copy(line.substr(0, pos));
00212
00213 params[var] = value;
00214 }
00215
00216 return params;
00217 }
00218
00219 template<typename T>
00220 void JetMatchingMadgraph::updateOrDie(
00221 const std::map<std::string, std::string> ¶ms,
00222 T ¶m, const std::string &name)
00223 {
00224 if (param < 0){
00225 param = getParameter(params, name, param);
00226 }
00227 if (param < 0)
00228 throw cms::Exception("Generator|PartonShowerVeto")
00229 << "The MGParamCMS header does not specify the jet "
00230 "matching parameter \"" << name << "\", but it "
00231 "is requested by the CMSSW configuration."
00232 << std::endl;
00233 }
00234
00235
00236
00237 void JetMatchingMadgraph::init(const lhef::LHERunInfo* runInfo)
00238 {
00239
00240
00241 std::map<std::string, std::string> parameters;
00242
00243 std::vector<std::string> header = runInfo->findHeader("MGRunCard");
00244 if (header.empty())
00245 throw cms::Exception("Generator|PartonShowerVeto")
00246 << "In order to use MadGraph jet matching, "
00247 "the input file has to contain the corresponding "
00248 "MadGraph headers." << std::endl;
00249
00250 mgParams = parseHeader(header);
00251
00252
00253
00254 std::vector<Param> params;
00255 std::vector<Param> values;
00256 for(std::map<std::string, std::string>::const_iterator iter =
00257 mgParams.begin(); iter != mgParams.end(); ++iter) {
00258 params.push_back(" " + iter->first);
00259 values.push_back(iter->second);
00260
00261 }
00262
00263
00264
00265 uppriv_.ickkw = getParameter<int>("ickkw", 0);
00266 memain_.mektsc = getParameter<int>("ktscheme", 0);
00267
00268 header = runInfo->findHeader("MGParamCMS");
00269
00270 std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
00271
00272 for(std::map<std::string, std::string>::const_iterator iter =
00273 mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
00274 std::cout<<"mgInfoCMS: "<<iter->first<<" "<<iter->second<<std::endl;
00275
00276 }
00277
00278
00279 updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
00280 updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
00281 updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
00282 updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
00283 updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
00284 updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
00285
00286
00287
00288 int nparam = params.size();
00289 mginit_(&nparam, ¶ms.front(), &values.front());
00290 runInitialized = true;
00291 }
00292
00293
00294
00295
00296 void JetMatchingMadgraph::beforeHadronisation(const lhef::LHEEvent* event)
00297 {
00298 if (!runInitialized)
00299 throw cms::Exception("Generator|PartonShowerVeto")
00300 << "Run not initialized in JetMatchingMadgraph"
00301 << std::endl;
00302
00303
00304 if (uppriv_.ickkw)
00305 {
00306 std::vector<std::string> comments = event->getComments();
00307 if (comments.size() == 1)
00308 {
00309 std::istringstream ss(comments[0].substr(1));
00310 for(int i = 0; i < 1000; i++)
00311 {
00312 double pt;
00313 ss >> pt;
00314 if (!ss.good())
00315 break;
00316 pypart_.ptpart[i] = pt;
00317 }
00318 }
00319 else
00320 {
00321 edm::LogWarning("Generator|LHEInterface")
00322 << "Expected exactly one comment line per "
00323 "event containing MadGraph parton scale "
00324 "information."
00325 << std::endl;
00326
00327 const lhef::HEPEUP *hepeup = event->getHEPEUP();
00328 for(int i = 2; i < hepeup->NUP; i++)
00329 {
00330 double mt2 =
00331 hepeup->PUP[i][0] * hepeup->PUP[i][0] +
00332 hepeup->PUP[i][1] * hepeup->PUP[i][1] +
00333 hepeup->PUP[i][4] * hepeup->PUP[i][4];
00334 pypart_.ptpart[i - 2] = std::sqrt(mt2);
00335 }
00336 }
00337 }
00338
00339
00340 eventInitialized = true;
00341 }
00342
00343 void JetMatchingMadgraph::beforeHadronisationExec()
00344 {
00345 mgevnt_();
00346 eventInitialized = true;
00347 return;
00348 }
00349
00350
00351
00352
00353
00354
00355 int JetMatchingMadgraph::match( const lhef::LHEEvent* partonLevel, const std::vector<fastjet::PseudoJet>* jetInput )
00356 {
00357
00358
00359
00360
00361
00362
00363
00364 if (!runInitialized)
00365 throw cms::Exception("Generator|LHEInterface")
00366 << "Run not initialized in JetMatchingMadgraph"
00367 << std::endl;
00368
00369 if (!eventInitialized)
00370 throw cms::Exception("Generator|LHEInterface")
00371 << "Event not initialized in JetMatchingMadgraph"
00372 << std::endl;
00373
00374 if (soup)
00375 memaev_.iexc = (memaev_.nljets < memain_.maxjets);
00376 else
00377 memaev_.iexc = exclusive;
00378
00379 int veto = 0;
00380 mgveto_(&veto);
00381 fMatchingStatus=true;
00382 eventInitialized = false;
00383
00384 return veto;
00385 }
00386
00387 }