CMS 3D CMS Logo

JetMatchingMadgraph.cc
Go to the documentation of this file.
1 #include <functional>
2 #include <algorithm>
3 #include <iostream>
4 #include <sstream>
5 #include <vector>
6 #include <memory>
7 #include <cstring>
8 #include <string>
9 #include <cctype>
10 #include <map>
11 #include <set>
12 
13 #include <boost/algorithm/string/trim.hpp>
14 
15 // #include <HepMC/GenEvent.h>
16 
19 
23 
24 namespace gen {
25 
26  extern "C" {
27 #define PARAMLEN 20
28  namespace {
29  struct Param {
30  Param(const std::string &str) {
31  int len = std::min(PARAMLEN, (int)str.length());
32  std::memcpy(value, str.c_str(), len);
33  std::memset(value + len, ' ', PARAMLEN - len);
34  }
35 
36  char value[PARAMLEN];
37  };
38  } // namespace
39  extern void mginit_(int *npara, Param *params, Param *values);
40  extern void mgevnt_(void);
41  extern void mgveto_(int *veto);
42 
43  extern struct UPPRIV {
44  int lnhin, lnhout;
45  int mscal, ievnt;
46  int ickkw, iscale;
47  } uppriv_;
48 
49  extern struct MEMAIN {
52  int mektsc, nexcres, excres[30];
53  int nqmatch, excproc, iexcproc[1000], iexcval[1000];
55  } memain_;
56 
57  extern struct OUTTREE { int flag; } outtree_;
58 
59  extern struct MEMAEV {
60  double ptclus[20];
61  int nljets, iexc, ifile;
62  } memaev_;
63 
64  extern struct PYPART {
65  int npart, npartd, ipart[1000];
66  double ptpart[1000];
67  } pypart_;
68  } // extern "C"
69 
70  template <typename T>
72  std::istringstream ss(value);
73  T result;
74  ss >> result;
75  return result;
76  }
77 
78  template <>
81  if (!result.empty() && result[0] == '\'')
82  result = result.substr(1);
83  if (!result.empty() && result[result.length() - 1] == '\'')
84  result.resize(result.length() - 1);
85  return result;
86  }
87 
88  template <>
90  std::string value(value_);
91  std::transform(value.begin(), value.end(), value.begin(), (int (*)(int))std::toupper);
92  return value == "T" || value == "Y" || value == "True" || value == "1" || value == ".TRUE.";
93  }
94 
95  template <typename T>
96  T JetMatchingMadgraph::getParameter(const std::map<std::string, std::string> &params,
97  const std::string &var,
98  const T &defValue) {
99  std::map<std::string, std::string>::const_iterator pos = params.find(var);
100  if (pos == params.end())
101  return defValue;
102  return parseParameter<T>(pos->second);
103  }
104 
105  template <typename T>
106  T JetMatchingMadgraph::getParameter(const std::string &var, const T &defValue) const {
107  return getParameter(mgParams, var, defValue);
108  }
109 
111  : JetMatching(params), runInitialized(false) {
112  std::string mode = params.getParameter<std::string>("mode");
113  if (mode == "inclusive") {
114  soup = false;
115  exclusive = false;
116  } else if (mode == "exclusive") {
117  soup = false;
118  exclusive = true;
119  } else if (mode == "auto")
120  soup = true;
121  else
122  throw cms::Exception("Generator|LHEInterface") << "Madgraph jet matching scheme requires \"mode\" "
123  "parameter to be set to either \"inclusive\", "
124  "\"exclusive\" or \"auto\"."
125  << std::endl;
126 
127  memain_.etcjet = 0.;
128  memain_.rclmax = 0.0;
129  memain_.clfact = 0.0;
130  memain_.ktsche = 0.0;
131  memain_.etaclmax = params.getParameter<double>("MEMAIN_etaclmax");
132  memain_.qcut = params.getParameter<double>("MEMAIN_qcut");
133  memain_.minjets = params.getParameter<int>("MEMAIN_minjets");
134  memain_.maxjets = params.getParameter<int>("MEMAIN_maxjets");
135  memain_.showerkt = params.getParameter<double>("MEMAIN_showerkt");
136  memain_.nqmatch = params.getParameter<int>("MEMAIN_nqmatch");
137  outtree_.flag = params.getParameter<int>("outTree_flag");
138  std::string list_excres = params.getParameter<std::string>("MEMAIN_excres");
139  std::vector<std::string> elems;
140  std::stringstream ss(list_excres);
142  int index = 0;
143  while (std::getline(ss, item, ',')) {
144  elems.push_back(item);
145  memain_.excres[index] = std::atoi(item.c_str());
146  index++;
147  }
149  }
150 
152 
154 
155  std::set<std::string> JetMatchingMadgraph::capabilities() const {
156  std::set<std::string> result;
157  result.insert("psFinalState");
158  result.insert("hepevt");
159  result.insert("pythia6");
160  return result;
161  }
162 
163  static std::map<std::string, std::string> parseHeader(const std::vector<std::string> &header) {
164  std::map<std::string, std::string> params;
165 
166  for (std::vector<std::string>::const_iterator iter = header.begin(); iter != header.end(); ++iter) {
167  std::string line = *iter;
168  if (line.empty() || line[0] == '#')
169  continue;
170 
171  std::string::size_type pos = line.find('!');
172  if (pos != std::string::npos)
173  line.resize(pos);
174 
175  pos = line.find('=');
176  if (pos == std::string::npos)
177  continue;
178 
179  std::string var = boost::algorithm::trim_copy(line.substr(pos + 1));
180  std::string value = boost::algorithm::trim_copy(line.substr(0, pos));
181 
182  params[var] = value;
183  }
184 
185  return params;
186  }
187 
188  template <typename T>
189  void JetMatchingMadgraph::updateOrDie(const std::map<std::string, std::string> &params,
190  T &param,
191  const std::string &name) {
192  if (param < 0) {
193  param = getParameter(params, name, param);
194  }
195  if (param < 0)
196  throw cms::Exception("Generator|PartonShowerVeto") << "The MGParamCMS header does not specify the jet "
197  "matching parameter \""
198  << name
199  << "\", but it "
200  "is requested by the CMSSW configuration."
201  << std::endl;
202  }
203 
204  // implements the Madgraph method - use ME2pythia.f
205 
207  // read MadGraph run card
208 
209  std::map<std::string, std::string> parameters;
210 
211  std::vector<std::string> header = runInfo->findHeader("MGRunCard");
212  if (header.empty())
213  throw cms::Exception("Generator|PartonShowerVeto") << "In order to use MadGraph jet matching, "
214  "the input file has to contain the corresponding "
215  "MadGraph headers."
216  << std::endl;
217 
219 
220  // set variables in common block
221 
222  std::vector<Param> params;
223  std::vector<Param> values;
224  for (std::map<std::string, std::string>::const_iterator iter = mgParams.begin(); iter != mgParams.end(); ++iter) {
225  params.push_back(" " + iter->first);
226  values.push_back(iter->second);
227  }
228 
229  // set MG matching parameters
230 
231  uppriv_.ickkw = getParameter<int>("ickkw", 0);
232  memain_.mektsc = getParameter<int>("ktscheme", 0);
233 
234  header = runInfo->findHeader("MGParamCMS");
235 
236  std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
237 
238  for (std::map<std::string, std::string>::const_iterator iter = mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
239  std::cout << "mgInfoCMS: " << iter->first << " " << iter->second << std::endl;
240  }
241 
242  updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
243  updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
244  updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
245  updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
246  updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
247  updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
248 
249  // run Fortran initialization code
250 
251  int nparam = params.size();
252  mginit_(&nparam, &params.front(), &values.front());
253  runInitialized = true;
254  }
255 
256  //void JetMatchingMadgraph::beforeHadronisation(
257  // const std::shared_ptr<lhef::LHEEvent> &event)
258 
260  if (!runInitialized)
261  throw cms::Exception("Generator|PartonShowerVeto") << "Run not initialized in JetMatchingMadgraph" << std::endl;
262 
263  if (uppriv_.ickkw) {
264  std::vector<std::string> comments = event->getComments();
265  if (comments.size() == 1) {
266  std::istringstream ss(comments[0].substr(1));
267  for (int i = 0; i < 1000; i++) {
268  double pt;
269  ss >> pt;
270  if (!ss.good())
271  break;
272  pypart_.ptpart[i] = pt;
273  }
274  } else {
275  edm::LogWarning("Generator|LHEInterface") << "Expected exactly one comment line per "
276  "event containing MadGraph parton scale "
277  "information."
278  << std::endl;
279 
280  const lhef::HEPEUP *hepeup = event->getHEPEUP();
281  for (int i = 2; i < hepeup->NUP; i++) {
282  double mt2 = hepeup->PUP[i][0] * hepeup->PUP[i][0] + hepeup->PUP[i][1] * hepeup->PUP[i][1] +
283  hepeup->PUP[i][4] * hepeup->PUP[i][4];
284  pypart_.ptpart[i - 2] = std::sqrt(mt2);
285  }
286  }
287  }
288 
289  // mgevnt_();
290  eventInitialized = true;
291  }
292 
294  mgevnt_();
295  eventInitialized = true;
296  return;
297  }
298 
299  /*
300 int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
301  const HepMC::GenEvent *finalState,
302  bool showeredFinalState)
303 */
304  int JetMatchingMadgraph::match(const lhef::LHEEvent *partonLevel, const std::vector<fastjet::PseudoJet> *jetInput) {
305  /*
306  if (!showeredFinalState)
307  throw cms::Exception("Generator|LHEInterface")
308  << "MadGraph matching expected parton shower "
309  "final state." << std::endl;
310 */
311 
312  if (!runInitialized)
313  throw cms::Exception("Generator|LHEInterface") << "Run not initialized in JetMatchingMadgraph" << std::endl;
314 
315  if (!eventInitialized)
316  throw cms::Exception("Generator|LHEInterface") << "Event not initialized in JetMatchingMadgraph" << std::endl;
317 
318  if (soup)
320  else
322 
323  int veto = 0;
324  mgveto_(&veto);
325  fMatchingStatus = true;
326  eventInitialized = false;
327 
328  return veto;
329  }
330 
331 } // end namespace gen
struct gen::OUTTREE outtree_
struct gen::MEMAIN memain_
struct gen::MEMAEV memaev_
std::set< std::string > capabilities() const override
void init(const lhef::LHERunInfo *runInfo) override
struct gen::PYPART pypart_
void beforeHadronisation(const lhef::LHEEvent *event) override
double ptpart[1000]
static T parseParameter(const std::string &value)
static void updateOrDie(const std::map< std::string, std::string > &params, T &param, const std::string &name)
uint16_t size_type
static T getParameter(const std::map< std::string, std::string > &params, const std::string &var, const T &defValue=T())
int match(const lhef::LHEEvent *partonLevel, const std::vector< fastjet::PseudoJet > *jetInput) override
T sqrt(T t)
Definition: SSEVec.h:19
std::vector< FiveVector > PUP
Definition: LesHouches.h:246
Definition: value.py:1
void mgevnt_(void)
static std::map< std::string, std::string > parseHeader(const std::vector< std::string > &header)
JetMatchingMadgraph(const edm::ParameterSet &params)
void beforeHadronisationExec() override
std::map< std::string, std::string > mgParams
void mginit_(int *npara, Param *params, Param *values)
#define PARAMLEN
void mgveto_(int *veto)
Log< level::Warning, false > LogWarning
#define str(s)
long double T
struct gen::UPPRIV uppriv_
double getJetEtaMax() const override
Definition: event.py:1
unsigned transform(const HcalDetId &id, unsigned transformCode)