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 {
58  int flag;
59  } outtree_;
60 
61  extern struct MEMAEV {
62  double ptclus[20];
63  int nljets, iexc, ifile;
64  } memaev_;
65 
66  extern struct PYPART {
67  int npart, npartd, ipart[1000];
68  double ptpart[1000];
69  } pypart_;
70  } // extern "C"
71 
72  template <typename T>
74  std::istringstream ss(value);
75  T result;
76  ss >> result;
77  return result;
78  }
79 
80  template <>
83  if (!result.empty() && result[0] == '\'')
84  result = result.substr(1);
85  if (!result.empty() && result[result.length() - 1] == '\'')
86  result.resize(result.length() - 1);
87  return result;
88  }
89 
90  template <>
92  std::string value(value_);
93  std::transform(value.begin(), value.end(), value.begin(), (int (*)(int))std::toupper);
94  return value == "T" || value == "Y" || value == "True" || value == "1" || value == ".TRUE.";
95  }
96 
97  template <typename T>
98  T JetMatchingMadgraph::getParameter(const std::map<std::string, std::string> &params,
99  const std::string &var,
100  const T &defValue) {
101  std::map<std::string, std::string>::const_iterator pos = params.find(var);
102  if (pos == params.end())
103  return defValue;
104  return parseParameter<T>(pos->second);
105  }
106 
107  template <typename T>
108  T JetMatchingMadgraph::getParameter(const std::string &var, const T &defValue) const {
109  return getParameter(mgParams, var, defValue);
110  }
111 
113  : JetMatching(params), runInitialized(false) {
114  std::string mode = params.getParameter<std::string>("mode");
115  if (mode == "inclusive") {
116  soup = false;
117  exclusive = false;
118  } else if (mode == "exclusive") {
119  soup = false;
120  exclusive = true;
121  } else if (mode == "auto")
122  soup = true;
123  else
124  throw cms::Exception("Generator|LHEInterface") << "Madgraph jet matching scheme requires \"mode\" "
125  "parameter to be set to either \"inclusive\", "
126  "\"exclusive\" or \"auto\"."
127  << std::endl;
128 
129  memain_.etcjet = 0.;
130  memain_.rclmax = 0.0;
131  memain_.clfact = 0.0;
132  memain_.ktsche = 0.0;
133  memain_.etaclmax = params.getParameter<double>("MEMAIN_etaclmax");
134  memain_.qcut = params.getParameter<double>("MEMAIN_qcut");
135  memain_.minjets = params.getParameter<int>("MEMAIN_minjets");
136  memain_.maxjets = params.getParameter<int>("MEMAIN_maxjets");
137  memain_.showerkt = params.getParameter<double>("MEMAIN_showerkt");
138  memain_.nqmatch = params.getParameter<int>("MEMAIN_nqmatch");
139  outtree_.flag = params.getParameter<int>("outTree_flag");
140  std::string list_excres = params.getParameter<std::string>("MEMAIN_excres");
141  std::vector<std::string> elems;
142  std::stringstream ss(list_excres);
144  int index = 0;
145  while (std::getline(ss, item, ',')) {
146  elems.push_back(item);
147  memain_.excres[index] = std::atoi(item.c_str());
148  index++;
149  }
151  }
152 
154 
156 
157  std::set<std::string> JetMatchingMadgraph::capabilities() const {
158  std::set<std::string> result;
159  result.insert("psFinalState");
160  result.insert("hepevt");
161  result.insert("pythia6");
162  return result;
163  }
164 
165  static std::map<std::string, std::string> parseHeader(const std::vector<std::string> &header) {
166  std::map<std::string, std::string> params;
167 
168  for (std::vector<std::string>::const_iterator iter = header.begin(); iter != header.end(); ++iter) {
169  std::string line = *iter;
170  if (line.empty() || line[0] == '#')
171  continue;
172 
173  std::string::size_type pos = line.find('!');
174  if (pos != std::string::npos)
175  line.resize(pos);
176 
177  pos = line.find('=');
178  if (pos == std::string::npos)
179  continue;
180 
181  std::string var = boost::algorithm::trim_copy(line.substr(pos + 1));
182  std::string value = boost::algorithm::trim_copy(line.substr(0, pos));
183 
184  params[var] = value;
185  }
186 
187  return params;
188  }
189 
190  template <typename T>
191  void JetMatchingMadgraph::updateOrDie(const std::map<std::string, std::string> &params,
192  T &param,
193  const std::string &name) {
194  if (param < 0) {
195  param = getParameter(params, name, param);
196  }
197  if (param < 0)
198  throw cms::Exception("Generator|PartonShowerVeto") << "The MGParamCMS header does not specify the jet "
199  "matching parameter \""
200  << name
201  << "\", but it "
202  "is requested by the CMSSW configuration."
203  << std::endl;
204  }
205 
206  // implements the Madgraph method - use ME2pythia.f
207 
209  // read MadGraph run card
210 
211  std::map<std::string, std::string> parameters;
212 
213  std::vector<std::string> header = runInfo->findHeader("MGRunCard");
214  if (header.empty())
215  throw cms::Exception("Generator|PartonShowerVeto") << "In order to use MadGraph jet matching, "
216  "the input file has to contain the corresponding "
217  "MadGraph headers."
218  << std::endl;
219 
221 
222  // set variables in common block
223 
224  std::vector<Param> params;
225  std::vector<Param> values;
226  for (std::map<std::string, std::string>::const_iterator iter = mgParams.begin(); iter != mgParams.end(); ++iter) {
227  params.push_back(" " + iter->first);
228  values.push_back(iter->second);
229  }
230 
231  // set MG matching parameters
232 
233  uppriv_.ickkw = getParameter<int>("ickkw", 0);
234  memain_.mektsc = getParameter<int>("ktscheme", 0);
235 
236  header = runInfo->findHeader("MGParamCMS");
237 
238  std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
239 
240  for (std::map<std::string, std::string>::const_iterator iter = mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
241  std::cout << "mgInfoCMS: " << iter->first << " " << iter->second << std::endl;
242  }
243 
244  updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
245  updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
246  updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
247  updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
248  updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
249  updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
250 
251  // run Fortran initialization code
252 
253  int nparam = params.size();
254  mginit_(&nparam, &params.front(), &values.front());
255  runInitialized = true;
256  }
257 
258  //void JetMatchingMadgraph::beforeHadronisation(
259  // const std::shared_ptr<lhef::LHEEvent> &event)
260 
262  if (!runInitialized)
263  throw cms::Exception("Generator|PartonShowerVeto") << "Run not initialized in JetMatchingMadgraph" << std::endl;
264 
265  if (uppriv_.ickkw) {
266  std::vector<std::string> comments = event->getComments();
267  if (comments.size() == 1) {
268  std::istringstream ss(comments[0].substr(1));
269  for (int i = 0; i < 1000; i++) {
270  double pt;
271  ss >> pt;
272  if (!ss.good())
273  break;
274  pypart_.ptpart[i] = pt;
275  }
276  } else {
277  edm::LogWarning("Generator|LHEInterface") << "Expected exactly one comment line per "
278  "event containing MadGraph parton scale "
279  "information."
280  << std::endl;
281 
282  const lhef::HEPEUP *hepeup = event->getHEPEUP();
283  for (int i = 2; i < hepeup->NUP; i++) {
284  double mt2 = hepeup->PUP[i][0] * hepeup->PUP[i][0] + hepeup->PUP[i][1] * hepeup->PUP[i][1] +
285  hepeup->PUP[i][4] * hepeup->PUP[i][4];
286  pypart_.ptpart[i - 2] = std::sqrt(mt2);
287  }
288  }
289  }
290 
291  // mgevnt_();
292  eventInitialized = true;
293  }
294 
296  mgevnt_();
297  eventInitialized = true;
298  return;
299  }
300 
301  /*
302 int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
303  const HepMC::GenEvent *finalState,
304  bool showeredFinalState)
305 */
306  int JetMatchingMadgraph::match(const lhef::LHEEvent *partonLevel, const std::vector<fastjet::PseudoJet> *jetInput) {
307  /*
308  if (!showeredFinalState)
309  throw cms::Exception("Generator|LHEInterface")
310  << "MadGraph matching expected parton shower "
311  "final state." << std::endl;
312 */
313 
314  if (!runInitialized)
315  throw cms::Exception("Generator|LHEInterface") << "Run not initialized in JetMatchingMadgraph" << std::endl;
316 
317  if (!eventInitialized)
318  throw cms::Exception("Generator|LHEInterface") << "Event not initialized in JetMatchingMadgraph" << std::endl;
319 
320  if (soup)
322  else
324 
325  int veto = 0;
326  mgveto_(&veto);
327  fMatchingStatus = true;
328  eventInitialized = false;
329 
330  return veto;
331  }
332 
333 } // 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:23
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)