CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/GeneratorInterface/PartonShowerVeto/src/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 <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 #include <HepMC/GenEvent.h>
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 } // extern "C"
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> &params,
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 &params) :
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 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 static std::map<std::string, std::string>
00185 parseHeader(const std::vector<std::string> &header)
00186 {
00187         std::map<std::string, std::string> params;
00188 
00189         for(std::vector<std::string>::const_iterator iter = header.begin();
00190             iter != header.end(); ++iter) {
00191                 std::string line = *iter;
00192                 if (line.empty() || line[0] == '#')
00193                         continue;
00194 
00195                 std::string::size_type pos = line.find('!');
00196                 if (pos != std::string::npos)
00197                         line.resize(pos);
00198 
00199                 pos = line.find('=');
00200                 if (pos == std::string::npos)
00201                         continue;
00202 
00203                 std::string var =
00204                         boost::algorithm::trim_copy(line.substr(pos + 1));
00205                 std::string value = 
00206                         boost::algorithm::trim_copy(line.substr(0, pos));
00207 
00208                 params[var] = value;
00209         }
00210 
00211         return params;
00212 }
00213 
00214 template<typename T>
00215 void JetMatchingMadgraph::updateOrDie(
00216                         const std::map<std::string, std::string> &params,
00217                         T &param, const std::string &name)
00218 {
00219         if (param < 0){
00220                 param = getParameter(params, name, param);
00221         }
00222         if (param < 0)
00223                 throw cms::Exception("Generator|PartonShowerVeto")
00224                         << "The MGParamCMS header does not specify the jet "
00225                            "matching parameter \"" << name << "\", but it "
00226                            "is requested by the CMSSW configuration."
00227                         << std::endl;
00228 }
00229 
00230 // implements the Madgraph method - use ME2pythia.f
00231 
00232 void JetMatchingMadgraph::init(const lhef::LHERunInfo* runInfo)
00233 {
00234         // read MadGraph run card
00235 
00236         std::map<std::string, std::string> parameters;
00237 
00238         std::vector<std::string> header = runInfo->findHeader("MGRunCard");
00239         if (header.empty())
00240                 throw cms::Exception("Generator|PartonShowerVeto")
00241                         << "In order to use MadGraph jet matching, "
00242                            "the input file has to contain the corresponding "
00243                            "MadGraph headers." << std::endl;
00244 
00245         mgParams = parseHeader(header);
00246 
00247         // set variables in common block
00248 
00249         std::vector<Param> params;
00250         std::vector<Param> values;
00251         for(std::map<std::string, std::string>::const_iterator iter =
00252                         mgParams.begin(); iter != mgParams.end(); ++iter) {
00253                 params.push_back(" " + iter->first);
00254                 values.push_back(iter->second);
00255 
00256         }
00257 
00258         // set MG matching parameters
00259 
00260         uppriv_.ickkw = getParameter<int>("ickkw", 0);
00261         memain_.mektsc = getParameter<int>("ktscheme", 0);
00262 
00263         header = runInfo->findHeader("MGParamCMS");
00264         
00265         std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
00266 
00267         for(std::map<std::string, std::string>::const_iterator iter =
00268                         mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
00269                         std::cout<<"mgInfoCMS: "<<iter->first<<" "<<iter->second<<std::endl;
00270 
00271         }
00272 
00273 
00274         updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
00275         updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
00276         updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
00277         updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
00278         updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
00279         updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
00280 
00281         // run Fortran initialization code
00282 
00283         int nparam = params.size();
00284         mginit_(&nparam, &params.front(), &values.front());
00285         runInitialized = true;
00286 }
00287 
00288 //void JetMatchingMadgraph::beforeHadronisation(
00289 //                              const boost::shared_ptr<lhef::LHEEvent> &event)
00290 
00291 void JetMatchingMadgraph::beforeHadronisation(const lhef::LHEEvent* event)
00292 {
00293         if (!runInitialized)
00294                 throw cms::Exception("Generator|PartonShowerVeto")
00295                         << "Run not initialized in JetMatchingMadgraph"
00296                         << std::endl;
00297 
00298 
00299         if (uppriv_.ickkw) {
00300                 std::vector<std::string> comments = event->getComments();
00301                 if (comments.size() == 1) {
00302                         std::istringstream ss(comments[0].substr(1));
00303                         for(int i = 0; i < 1000; i++) {
00304                                 double pt;
00305                                 ss >> pt;
00306                                 if (!ss.good())
00307                                         break;
00308                                 pypart_.ptpart[i] = pt;
00309                         }
00310                 } else {
00311                         edm::LogWarning("Generator|LHEInterface")
00312                                 << "Expected exactly one comment line per "
00313                                    "event containing MadGraph parton scale "
00314                                    "information."
00315                                 << std::endl;
00316 
00317                         const lhef::HEPEUP *hepeup = event->getHEPEUP();
00318                         for(int i = 2; i < hepeup->NUP; i++) {
00319                                 double mt2 =
00320                                         hepeup->PUP[i][0] * hepeup->PUP[i][0] +
00321                                         hepeup->PUP[i][1] * hepeup->PUP[i][1] +
00322                                         hepeup->PUP[i][4] * hepeup->PUP[i][4];
00323                                 pypart_.ptpart[i - 2] = std::sqrt(mt2);
00324                         }
00325                 }
00326         }
00327 
00328         // mgevnt_();
00329         eventInitialized = true;
00330 }
00331 
00332 void JetMatchingMadgraph::beforeHadronisationExec()
00333 {
00334     mgevnt_();
00335         eventInitialized = true;
00336         return;
00337 }
00338 
00339 int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
00340                                   const HepMC::GenEvent *finalState,
00341                                   bool showeredFinalState)
00342 {
00343         if (!showeredFinalState)
00344                 throw cms::Exception("Generator|LHEInterface")
00345                         << "MadGraph matching expected parton shower "
00346                            "final state." << std::endl;
00347 
00348         if (!runInitialized)
00349                 throw cms::Exception("Generator|LHEInterface")
00350                         << "Run not initialized in JetMatchingMadgraph"
00351                         << std::endl;
00352 
00353         if (!eventInitialized)
00354                 throw cms::Exception("Generator|LHEInterface")
00355                         << "Event not initialized in JetMatchingMadgraph"
00356                         << std::endl;
00357 
00358         if (soup)
00359                 memaev_.iexc = (memaev_.nljets < memain_.maxjets);
00360         else
00361                 memaev_.iexc = exclusive;
00362 
00363         int veto = 0;
00364         mgveto_(&veto);
00365         fMatchingStatus=true;
00366         eventInitialized = false;
00367 
00368         return veto;
00369 }
00370 
00371 } // end namespace gen