Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:16

0001 #include <algorithm>
0002 #include <iostream>
0003 #include <sstream>
0004 #include <fstream>
0005 #include <string>
0006 #include <memory>
0007 #include <map>
0008 
0009 #include <boost/algorithm/string/classification.hpp>
0010 #include <boost/algorithm/string/split.hpp>
0011 #include <boost/algorithm/string/trim.hpp>
0012 
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 
0015 #include "GeneratorInterface/AlpgenInterface/interface/AlpgenHeader.h"
0016 
0017 namespace {
0018   struct AlpgenParTokens {
0019     unsigned int index;
0020     std::vector<double> values;
0021     std::vector<std::string> comments;
0022 
0023     bool parse(const std::string &line, bool withIndex);
0024   };
0025 }  // namespace
0026 
0027 bool AlpgenParTokens::parse(const std::string &line, bool withIndex) {
0028   std::string::size_type pos = line.find('!');
0029   if (pos == std::string::npos)
0030     return false;
0031 
0032   std::string tmp = boost::trim_copy(line.substr(0, pos));
0033   boost::split(comments, tmp, boost::algorithm::is_space(), boost::token_compress_on);
0034   if (comments.empty())
0035     return false;
0036 
0037   unsigned int i = 0, n = comments.size();
0038   if (withIndex) {
0039     std::istringstream ss(comments[i++]);
0040     ss >> index;
0041     if (ss.bad() || ss.peek() != std::istringstream::traits_type::eof())
0042       return false;
0043   }
0044 
0045   values.clear();
0046   while (i < n) {
0047     std::istringstream ss(comments[i++]);
0048     double value;
0049     ss >> value;
0050     if (ss.bad() || ss.peek() != std::istringstream::traits_type::eof())
0051       return false;
0052 
0053     values.push_back(value);
0054   }
0055 
0056   tmp = boost::trim_copy(line.substr(pos + 1));
0057   boost::split(comments, tmp, boost::algorithm::is_space(), boost::token_compress_on);
0058 
0059   return true;
0060 }
0061 
0062 bool AlpgenHeader::parse(const std::vector<std::string>::const_iterator &begin,
0063                          const std::vector<std::string>::const_iterator &end) {
0064   std::vector<std::string>::const_iterator line = begin;
0065 
0066   // Mimicking Alpgen - read the _unw.par file until you find the "****".
0067   while (line != end)
0068     if ((line++)->find("****") != std::string::npos)
0069       break;
0070 
0071   AlpgenParTokens tokens;
0072 
0073   // hard process code
0074   if (line == end || !tokens.parse(*line++, true) || !tokens.values.empty())
0075     return false;
0076   ihrd = tokens.index;
0077 
0078   // mc,mb,mt,mw,mz,mh
0079   if (line == end || !tokens.parse(*line++, false) || tokens.values.size() < 6)
0080     return false;
0081 
0082   std::copy(tokens.values.begin(), tokens.values.begin() + 6, masses);
0083 
0084   // key - value pairs
0085   params.clear();
0086   while (line != end && line->find("****") == std::string::npos) {
0087     if (!tokens.parse(*line++, true) || tokens.values.size() != 1)
0088       return false;
0089     params[(Parameter)tokens.index] = tokens.values[0];
0090   }
0091   if (line == end)
0092     return false;
0093   else
0094     line++;
0095 
0096   // cross-section
0097   if (line == end || !tokens.parse(*line++, false) || tokens.values.size() != 2)
0098     return false;
0099 
0100   xsec = tokens.values[0];
0101   xsecErr = tokens.values[1];
0102 
0103   // unweighted events, luminosity
0104   if (line == end || !tokens.parse(*line++, true) || tokens.values.size() != 1)
0105     return false;
0106 
0107   nEvents = tokens.index;
0108   lumi = tokens.values[0];
0109 
0110   return true;
0111 }
0112 
0113 // create human-readable representation for all Alpgen parameter indices
0114 
0115 #define DEFINE_ALPGEN_PARAMETER(x) \
0116   { AlpgenHeader::x, #x }
0117 
0118 namespace {
0119   struct AlpgenParameterName {
0120     AlpgenHeader::Parameter index;
0121     const char *name;
0122 
0123     inline bool operator==(AlpgenHeader::Parameter index) const { return this->index == index; }
0124   }
0125 
0126   const alpgenParameterNames[] = {
0127       DEFINE_ALPGEN_PARAMETER(ih2),      DEFINE_ALPGEN_PARAMETER(ebeam),   DEFINE_ALPGEN_PARAMETER(ndns),
0128       DEFINE_ALPGEN_PARAMETER(iqopt),    DEFINE_ALPGEN_PARAMETER(qfac),    DEFINE_ALPGEN_PARAMETER(ickkw),
0129       DEFINE_ALPGEN_PARAMETER(ktfac),    DEFINE_ALPGEN_PARAMETER(njets),   DEFINE_ALPGEN_PARAMETER(ihvy),
0130       DEFINE_ALPGEN_PARAMETER(ihvy2),    DEFINE_ALPGEN_PARAMETER(nw),      DEFINE_ALPGEN_PARAMETER(nz),
0131       DEFINE_ALPGEN_PARAMETER(nh),       DEFINE_ALPGEN_PARAMETER(nph),     DEFINE_ALPGEN_PARAMETER(ptjmin),
0132       DEFINE_ALPGEN_PARAMETER(ptbmin),   DEFINE_ALPGEN_PARAMETER(ptcmin),  DEFINE_ALPGEN_PARAMETER(ptlmin),
0133       DEFINE_ALPGEN_PARAMETER(metmin),   DEFINE_ALPGEN_PARAMETER(ptphmin), DEFINE_ALPGEN_PARAMETER(etajmax),
0134       DEFINE_ALPGEN_PARAMETER(etabmax),  DEFINE_ALPGEN_PARAMETER(etacmax), DEFINE_ALPGEN_PARAMETER(etalmax),
0135       DEFINE_ALPGEN_PARAMETER(etaphmax), DEFINE_ALPGEN_PARAMETER(drjmin),  DEFINE_ALPGEN_PARAMETER(drbmin),
0136       DEFINE_ALPGEN_PARAMETER(drcmin),   DEFINE_ALPGEN_PARAMETER(drlmin),  DEFINE_ALPGEN_PARAMETER(drphjmin),
0137       DEFINE_ALPGEN_PARAMETER(drphlmin), DEFINE_ALPGEN_PARAMETER(drphmin), DEFINE_ALPGEN_PARAMETER(mllmin),
0138       DEFINE_ALPGEN_PARAMETER(mllmax),   DEFINE_ALPGEN_PARAMETER(iseed1),  DEFINE_ALPGEN_PARAMETER(iseed2),
0139       DEFINE_ALPGEN_PARAMETER(itopprc),  DEFINE_ALPGEN_PARAMETER(cluopt),  DEFINE_ALPGEN_PARAMETER(iseed3),
0140       DEFINE_ALPGEN_PARAMETER(iseed4)};
0141 }  // anonymous namespace
0142 
0143 std::string AlpgenHeader::parameterName(Parameter index) {
0144   static const unsigned int size = sizeof alpgenParameterNames / sizeof alpgenParameterNames[0];
0145 
0146   const AlpgenParameterName *pos = std::find(alpgenParameterNames, alpgenParameterNames + size, index);
0147 
0148   if (pos != alpgenParameterNames + size)
0149     return pos->name;
0150 
0151   std::ostringstream ss;
0152   ss << "unknown " << (int)index;
0153   return ss.str();
0154 }