CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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/shared_ptr.hpp>
14 #include <boost/algorithm/string/trim.hpp>
15 
16 #include <HepMC/GenEvent.h>
17 
20 
24 
25 namespace gen {
26 
27 extern "C" {
28  #define PARAMLEN 20
29  namespace {
30  struct Param {
31  Param(const std::string &str)
32  {
33  int len = std::min(PARAMLEN,
34  (int)str.length());
35  std::memcpy(value, str.c_str(), len);
36  std::memset(value + len, ' ', PARAMLEN - len);
37  }
38 
39  char value[PARAMLEN];
40  };
41  }
42  extern void mginit_(int *npara, Param *params, Param *values);
43  extern void mgevnt_(void);
44  extern void mgveto_(int *veto);
45 
46  extern struct UPPRIV {
47  int lnhin, lnhout;
48  int mscal, ievnt;
49  int ickkw, iscale;
50  } uppriv_;
51 
52  extern struct MEMAIN {
55  int mektsc,nexcres, excres[30];
56  int nqmatch,excproc,iexcproc[1000],iexcval[1000];
58  } memain_;
59 
60  extern struct OUTTREE {
61  int flag;
62  } outtree_;
63 
64  extern struct MEMAEV {
65  double ptclus[20];
66  int nljets, iexc, ifile;
67  } memaev_;
68 
69  extern struct PYPART {
70  int npart, npartd, ipart[1000];
71  double ptpart[1000];
72  } pypart_;
73 } // extern "C"
74 
75 
76 template<typename T>
78 {
79  std::istringstream ss(value);
80  T result;
81  ss >> result;
82  return result;
83 }
84 
85 template<>
86 std::string JetMatchingMadgraph::parseParameter(const std::string &value)
87 {
88  std::string result;
89  if (!result.empty() && result[0] == '\'')
90  result = result.substr(1);
91  if (!result.empty() && result[result.length() - 1] == '\'')
92  result.resize(result.length() - 1);
93  return result;
94 }
95 
96 template<>
97 bool JetMatchingMadgraph::parseParameter(const std::string &value_)
98 {
99  std::string value(value_);
100  std::transform(value.begin(), value.end(),
101  value.begin(), (int(*)(int))std::toupper);
102  return value == "T" || value == "Y" || value=="True" ||
103  value == "1" || value == ".TRUE.";
104 }
105 
106 template<typename T>
108  const std::map<std::string, std::string> &params,
109  const std::string &var, const T &defValue)
110 {
111  std::map<std::string, std::string>::const_iterator pos =
112  params.find(var);
113  if (pos == params.end())
114  return defValue;
115  return parseParameter<T>(pos->second);
116 }
117 
118 template<typename T>
119 T JetMatchingMadgraph::getParameter(const std::string &var,
120  const T &defValue) const
121 {
122  return getParameter(mgParams, var, defValue);
123 }
124 
126  JetMatching(params),
127  runInitialized(false)
128 {
129  std::string mode = params.getParameter<std::string>("mode");
130  if (mode == "inclusive") {
131  soup = false;
132  exclusive = false;
133  } else if (mode == "exclusive") {
134  soup = false;
135  exclusive = true;
136  } else if (mode == "auto")
137  soup = true;
138  else
139  throw cms::Exception("Generator|LHEInterface")
140  << "Madgraph jet matching scheme requires \"mode\" "
141  "parameter to be set to either \"inclusive\", "
142  "\"exclusive\" or \"auto\"." << std::endl;
143 
144  memain_.etcjet = 0.;
145  memain_.rclmax = 0.0;
146  memain_.clfact = 0.0;
147  memain_.ktsche = 0.0;
148  memain_.etaclmax = params.getParameter<double>("MEMAIN_etaclmax");
149  memain_.qcut = params.getParameter<double>("MEMAIN_qcut");
150  memain_.minjets = params.getParameter<int>("MEMAIN_minjets");
151  memain_.maxjets = params.getParameter<int>("MEMAIN_maxjets");
152  memain_.showerkt = params.getParameter<double>("MEMAIN_showerkt");
153  memain_.nqmatch = params.getParameter<int>("MEMAIN_nqmatch");
154  outtree_.flag = params.getParameter<int>("outTree_flag");
155  std::string list_excres = params.getParameter<std::string>("MEMAIN_excres");
156  std::vector<std::string> elems;
157  std::stringstream ss(list_excres);
158  std::string item;
159  int index=0;
160  while(std::getline(ss, item, ',')) {
161  elems.push_back(item);
162  memain_.excres[index]=std::atoi(item.c_str());
163  index++;
164  }
166 
167 
168 
169 }
170 
172 {
173 }
174 
175 std::set<std::string> JetMatchingMadgraph::capabilities() const
176 {
177  std::set<std::string> result;
178  result.insert("psFinalState");
179  result.insert("hepevt");
180  result.insert("pythia6");
181  return result;
182 }
183 
184 static std::map<std::string, std::string>
185 parseHeader(const std::vector<std::string> &header)
186 {
187  std::map<std::string, std::string> params;
188 
189  for(std::vector<std::string>::const_iterator iter = header.begin();
190  iter != header.end(); ++iter) {
191  std::string line = *iter;
192  if (line.empty() || line[0] == '#')
193  continue;
194 
195  std::string::size_type pos = line.find('!');
196  if (pos != std::string::npos)
197  line.resize(pos);
198 
199  pos = line.find('=');
200  if (pos == std::string::npos)
201  continue;
202 
203  std::string var =
204  boost::algorithm::trim_copy(line.substr(pos + 1));
205  std::string value =
206  boost::algorithm::trim_copy(line.substr(0, pos));
207 
208  params[var] = value;
209  }
210 
211  return params;
212 }
213 
214 template<typename T>
216  const std::map<std::string, std::string> &params,
217  T &param, const std::string &name)
218 {
219  if (param < 0){
220  param = getParameter(params, name, param);
221  }
222  if (param < 0)
223  throw cms::Exception("Generator|PartonShowerVeto")
224  << "The MGParamCMS header does not specify the jet "
225  "matching parameter \"" << name << "\", but it "
226  "is requested by the CMSSW configuration."
227  << std::endl;
228 }
229 
230 // implements the Madgraph method - use ME2pythia.f
231 
233 {
234  // read MadGraph run card
235 
236  std::map<std::string, std::string> parameters;
237 
238  std::vector<std::string> header = runInfo->findHeader("MGRunCard");
239  if (header.empty())
240  throw cms::Exception("Generator|PartonShowerVeto")
241  << "In order to use MadGraph jet matching, "
242  "the input file has to contain the corresponding "
243  "MadGraph headers." << std::endl;
244 
245  mgParams = parseHeader(header);
246 
247  // set variables in common block
248 
249  std::vector<Param> params;
250  std::vector<Param> values;
251  for(std::map<std::string, std::string>::const_iterator iter =
252  mgParams.begin(); iter != mgParams.end(); ++iter) {
253  params.push_back(" " + iter->first);
254  values.push_back(iter->second);
255 
256  }
257 
258  // set MG matching parameters
259 
260  uppriv_.ickkw = getParameter<int>("ickkw", 0);
261  memain_.mektsc = getParameter<int>("ktscheme", 0);
262 
263  header = runInfo->findHeader("MGParamCMS");
264 
265  std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
266 
267  for(std::map<std::string, std::string>::const_iterator iter =
268  mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
269  std::cout<<"mgInfoCMS: "<<iter->first<<" "<<iter->second<<std::endl;
270 
271  }
272 
273 
274  updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
275  updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
276  updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
277  updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
278  updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
279  updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
280 
281  // run Fortran initialization code
282 
283  int nparam = params.size();
284  mginit_(&nparam, &params.front(), &values.front());
285  runInitialized = true;
286 }
287 
288 //void JetMatchingMadgraph::beforeHadronisation(
289 // const boost::shared_ptr<lhef::LHEEvent> &event)
290 
292 {
293  if (!runInitialized)
294  throw cms::Exception("Generator|PartonShowerVeto")
295  << "Run not initialized in JetMatchingMadgraph"
296  << std::endl;
297 
298 
299  if (uppriv_.ickkw) {
300  std::vector<std::string> comments = event->getComments();
301  if (comments.size() == 1) {
302  std::istringstream ss(comments[0].substr(1));
303  for(int i = 0; i < 1000; i++) {
304  double pt;
305  ss >> pt;
306  if (!ss.good())
307  break;
308  pypart_.ptpart[i] = pt;
309  }
310  } else {
311  edm::LogWarning("Generator|LHEInterface")
312  << "Expected exactly one comment line per "
313  "event containing MadGraph parton scale "
314  "information."
315  << std::endl;
316 
317  const lhef::HEPEUP *hepeup = event->getHEPEUP();
318  for(int i = 2; i < hepeup->NUP; i++) {
319  double mt2 =
320  hepeup->PUP[i][0] * hepeup->PUP[i][0] +
321  hepeup->PUP[i][1] * hepeup->PUP[i][1] +
322  hepeup->PUP[i][4] * hepeup->PUP[i][4];
323  pypart_.ptpart[i - 2] = std::sqrt(mt2);
324  }
325  }
326  }
327 
328  // mgevnt_();
329  eventInitialized = true;
330 }
331 
333 {
334  mgevnt_();
335  eventInitialized = true;
336  return;
337 }
338 
339 int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
340  const HepMC::GenEvent *finalState,
341  bool showeredFinalState)
342 {
343  if (!showeredFinalState)
344  throw cms::Exception("Generator|LHEInterface")
345  << "MadGraph matching expected parton shower "
346  "final state." << std::endl;
347 
348  if (!runInitialized)
349  throw cms::Exception("Generator|LHEInterface")
350  << "Run not initialized in JetMatchingMadgraph"
351  << std::endl;
352 
353  if (!eventInitialized)
354  throw cms::Exception("Generator|LHEInterface")
355  << "Event not initialized in JetMatchingMadgraph"
356  << std::endl;
357 
358  if (soup)
360  else
362 
363  int veto = 0;
364  mgveto_(&veto);
365  fMatchingStatus=true;
366  eventInitialized = false;
367 
368  return veto;
369 }
370 
371 } // end namespace gen
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
struct gen::MEMAEV memaev_
struct gen::UPPRIV uppriv_
void init(const lhef::LHERunInfo *runInfo)
struct gen::PYPART pypart_
double ptpart[1000]
void beforeHadronisation(const lhef::LHEEvent *event)
static T parseParameter(const std::string &value)
#define min(a, b)
Definition: mlp_lapack.h:161
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())
T sqrt(T t)
Definition: SSEVec.h:46
std::vector< FiveVector > PUP
Definition: LesHouches.h:248
tuple result
Definition: query.py:137
int match(const HepMC::GenEvent *partonLevel, const HepMC::GenEvent *finalState, bool showeredFinalState)
struct gen::MEMAIN memain_
void mgevnt_(void)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
static std::map< std::string, std::string > parseHeader(const std::vector< std::string > &header)
JetMatchingMadgraph(const edm::ParameterSet &params)
std::map< std::string, std::string > mgParams
void mginit_(int *npara, Param *params, Param *values)
std::set< std::string > capabilities() const
#define PARAMLEN
void mgveto_(int *veto)
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:390
long double T
struct gen::OUTTREE outtree_