CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/GeneratorInterface/LHEInterface/plugins/JetMatchingMadgraph.cc

Go to the documentation of this file.
00001 #include <functional>
00002 #include <algorithm>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <vector>
00006 #include <memory>
00007 #include <string>
00008 #include <cctype>
00009 #include <map>
00010 #include <set>
00011 
00012 #include <boost/shared_ptr.hpp>
00013 #include <boost/algorithm/string/trim.hpp>
00014 
00015 #include <HepMC/GenEvent.h>
00016 
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 
00020 #include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
00021 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00022 #include "GeneratorInterface/LHEInterface/interface/JetMatching.h"
00023 
00024 namespace lhef {
00025 
00026 extern "C" {
00027         #define PARAMLEN 20
00028         namespace {
00029                 struct Param {
00030                         Param(const std::string &str)
00031                         {
00032                                 int len = std::min(PARAMLEN,
00033                                                    (int)str.length());
00034                                 std::memcpy(value, str.c_str(), len);
00035                                 std::memset(value + len, ' ', PARAMLEN - len);
00036                         }
00037 
00038                         char value[PARAMLEN];
00039                 };
00040         }
00041         extern void mginit_(int *npara, Param *params, Param *values);
00042         extern void mgevnt_(void);
00043         extern void mgveto_(int *veto);
00044 
00045         extern struct UPPRIV {
00046                 int     lnhin, lnhout;
00047                 int     mscal, ievnt;
00048                 int     ickkw, iscale;
00049         } uppriv_;
00050 
00051         extern struct MEMAIN {
00052                 double  etcjet, rclmax, etaclmax, qcut, clfact;
00053                 int     maxjets, minjets, iexcfile, ktsche;
00054                 int     nexcres, excres[30];
00055         } memain_;
00056 
00057         extern struct MEMAEV {
00058                 double  ptclus[20];
00059                 int     nljets, iexc, ifile;
00060         } memaev_;
00061 
00062         extern struct PYPART {
00063                 int     npart, npartd, ipart[1000];
00064                 double  ptpart[1000];
00065         } pypart_;
00066 } // extern "C"
00067 
00068 class JetMatchingMadgraph : public JetMatching {
00069     public:
00070         JetMatchingMadgraph(const edm::ParameterSet &params);
00071         ~JetMatchingMadgraph();
00072 
00073     private:
00074         void init(const boost::shared_ptr<LHERunInfo> &runInfo);
00075         void beforeHadronisation(const boost::shared_ptr<LHEEvent> &event);
00076 
00077         double match(const HepMC::GenEvent *partonLevel,
00078                      const HepMC::GenEvent *finalState,
00079                      bool showeredFinalState);
00080 
00081         std::set<std::string> capabilities() const;
00082 
00083         template<typename T>
00084         static T parseParameter(const std::string &value);
00085         template<typename T>
00086         T getParameter(const std::string &var, const T &defValue = T()) const;
00087 
00088         std::map<std::string, std::string>      mgParams;
00089 
00090         bool                                    runInitialized;
00091         bool                                    eventInitialized;
00092         bool                                    soup;
00093         bool                                    exclusive;
00094 };
00095 
00096 template<typename T>
00097 T JetMatchingMadgraph::parseParameter(const std::string &value)
00098 {
00099         std::istringstream ss(value);
00100         T result;
00101         ss >> result;
00102         return result;
00103 }
00104 
00105 template<>
00106 std::string JetMatchingMadgraph::parseParameter(const std::string &value)
00107 {
00108         std::string result;
00109         if (!result.empty() && result[0] == '\'')
00110                 result = result.substr(1);
00111         if (!result.empty() && result[result.length() - 1] == '\'')
00112                 result.resize(result.length() - 1);
00113         return result;
00114 }
00115 
00116 template<>
00117 bool JetMatchingMadgraph::parseParameter(const std::string &value_)
00118 {
00119         std::string value(value_);
00120         std::transform(value.begin(), value.end(),
00121                        value.begin(), (int(*)(int))std::toupper);
00122         return value == "T" || value == "Y" ||
00123                value == "1" || value == ".TRUE.";
00124 }
00125 
00126 template<typename T>
00127 T JetMatchingMadgraph::getParameter(const std::string &var,
00128                                     const T &defValue) const
00129 {
00130         std::map<std::string, std::string>::const_iterator pos =
00131                                                         mgParams.find(var);
00132         if (pos == mgParams.end())
00133                 return defValue;
00134         return parseParameter<T>(pos->second);
00135 }
00136 
00137 } // namespace lhef
00138 
00139 using namespace lhef;
00140 
00141 JetMatchingMadgraph::JetMatchingMadgraph(const edm::ParameterSet &params) :
00142         JetMatching(params),
00143         runInitialized(false)
00144 {
00145         std::string mode = params.getParameter<std::string>("mode");
00146         if (mode == "inclusive") {
00147                 soup = false;
00148                 exclusive = false;
00149         } else if (mode == "exclusive") {
00150                 soup = false;
00151                 exclusive = true;
00152         } else if (mode == "auto")
00153                 soup = true;
00154         else
00155                 throw cms::Exception("Generator|LHEInterface")
00156                         << "Madgraph jet matching scheme requires \"mode\" "
00157                            "parameter to be set to either \"inclusive\", "
00158                            "\"exclusive\" or \"auto\"." << std::endl;
00159 
00160         memain_.etcjet = 0.;
00161         memain_.rclmax = 0.0;
00162         memain_.clfact = 0.0;
00163         memain_.iexcfile = 0;
00164         memain_.ktsche = 0;
00165         memain_.etaclmax = params.getParameter<double>("etaclmax");
00166         memain_.qcut = params.getParameter<double>("qcut");
00167         memain_.minjets = params.getParameter<int>("minjets");
00168         memain_.maxjets = params.getParameter<int>("maxjets");
00169 }
00170 
00171 JetMatchingMadgraph::~JetMatchingMadgraph()
00172 {
00173 }
00174 
00175 std::set<std::string> JetMatchingMadgraph::capabilities() const
00176 {
00177         std::set<std::string> result;
00178         result.insert("psFinalState");
00179         result.insert("hepevt");
00180         result.insert("pythia6");
00181         return result;
00182 }
00183 
00184 // implements the Madgraph method - use ME2pythia.f
00185 
00186 void JetMatchingMadgraph::init(const boost::shared_ptr<LHERunInfo> &runInfo)
00187 {
00188         // read MadGraph run card
00189 
00190         std::map<std::string, std::string> parameters;
00191 
00192         std::vector<std::string> header = runInfo->findHeader("MGRunCard");
00193         if (header.empty())
00194                 throw cms::Exception("Generator|LHEInterface")
00195                         << "In order to use MadGraph jet matching, "
00196                            "the input file has to contain the corresponding "
00197                            "MadGraph headers." << std::endl;
00198 
00199         mgParams.clear();
00200         for(std::vector<std::string>::const_iterator iter = header.begin();
00201             iter != header.end(); ++iter) {
00202                 std::string line = *iter;
00203                 if (line.empty() || line[0] == '#')
00204                         continue;
00205 
00206                 std::string::size_type pos = line.find('!');
00207                 if (pos != std::string::npos)
00208                         line.resize(pos);
00209 
00210                 pos = line.find('=');
00211                 if (pos == std::string::npos)
00212                         continue;
00213 
00214                 std::string var =
00215                         boost::algorithm::trim_copy(line.substr(pos + 1));
00216                 std::string value = 
00217                         boost::algorithm::trim_copy(line.substr(0, pos));
00218 
00219                 mgParams[var] = value;
00220         }
00221 
00222         // set variables in common block
00223 
00224         std::vector<Param> params;
00225         std::vector<Param> values;
00226         for(std::map<std::string, std::string>::const_iterator iter =
00227                         mgParams.begin(); iter != mgParams.end(); ++iter) {
00228                 params.push_back(" " + iter->first);
00229                 values.push_back(iter->second);
00230 
00231         }
00232 
00233         // set MG matching parameters
00234 
00235         uppriv_.ickkw = getParameter<int>("ickkw", 0);
00236 
00237         // run Fortran initialization code
00238 
00239         int nparam = params.size();
00240         mginit_(&nparam, &params.front(), &values.front());
00241         runInitialized = true;
00242 }
00243 
00244 void JetMatchingMadgraph::beforeHadronisation(
00245                                 const boost::shared_ptr<LHEEvent> &event)
00246 {
00247         if (!runInitialized)
00248                 throw cms::Exception("Generator|LHEInterface")
00249                         << "Run not initialized in JetMatchingMadgraph"
00250                         << std::endl;
00251 
00252         if (uppriv_.ickkw) {
00253                 std::vector<std::string> comments = event->getComments();
00254                 if (comments.size() == 1) {
00255                         std::istringstream ss(comments[0].substr(1));
00256                         for(int i = 0; i < 1000; i++) {
00257                                 double pt;
00258                                 ss >> pt;
00259                                 if (!ss.good())
00260                                         break;
00261                                 pypart_.ptpart[i] = pt;
00262                         }
00263                 } else {
00264                         edm::LogWarning("Generator|LHEInterface")
00265                                 << "Expected exactly one comment line per "
00266                                    "event containing MadGraph parton scale "
00267                                    "information."
00268                                 << std::endl;
00269 
00270                         const HEPEUP *hepeup = event->getHEPEUP();
00271                         for(int i = 2; i < hepeup->NUP; i++) {
00272                                 double mt2 =
00273                                         hepeup->PUP[i][0] * hepeup->PUP[i][0] +
00274                                         hepeup->PUP[i][1] * hepeup->PUP[i][1] +
00275                                         hepeup->PUP[i][4] * hepeup->PUP[i][4];
00276                                 pypart_.ptpart[i - 2] = std::sqrt(mt2);
00277                         }
00278                 }
00279         }
00280 
00281         mgevnt_();
00282         eventInitialized = true;
00283 }
00284 
00285 double JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
00286                                   const HepMC::GenEvent *finalState,
00287                                   bool showeredFinalState)
00288 {
00289         if (!showeredFinalState)
00290                 throw cms::Exception("Generator|LHEInterface")
00291                         << "MadGraph matching expected parton shower "
00292                            "final state." << std::endl;
00293 
00294         if (!runInitialized)
00295                 throw cms::Exception("Generator|LHEInterface")
00296                         << "Run not initialized in JetMatchingMadgraph"
00297                         << std::endl;
00298 
00299         if (!eventInitialized)
00300                 throw cms::Exception("Generator|LHEInterface")
00301                         << "Event not initialized in JetMatchingMadgraph"
00302                         << std::endl;
00303 
00304         if (soup)
00305                 memaev_.iexc = (memaev_.nljets < memain_.maxjets);
00306         else
00307                 memaev_.iexc = exclusive;
00308 
00309         int veto = 0;
00310         mgveto_(&veto);
00311         eventInitialized = false;
00312 
00313         return veto ? 0.0 : 1.0;
00314 }
00315 
00316 DEFINE_LHE_JETMATCHING_PLUGIN(JetMatchingMadgraph);