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<>
87 {
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<>
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>
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 
176 {
177  return memain_.etaclmax;
178 }
179 
180 std::set<std::string> JetMatchingMadgraph::capabilities() const
181 {
182  std::set<std::string> result;
183  result.insert("psFinalState");
184  result.insert("hepevt");
185  result.insert("pythia6");
186  return result;
187 }
188 
189 static std::map<std::string, std::string>
190 parseHeader(const std::vector<std::string> &header)
191 {
192  std::map<std::string, std::string> params;
193 
194  for(std::vector<std::string>::const_iterator iter = header.begin();
195  iter != header.end(); ++iter) {
196  std::string line = *iter;
197  if (line.empty() || line[0] == '#')
198  continue;
199 
200  std::string::size_type pos = line.find('!');
201  if (pos != std::string::npos)
202  line.resize(pos);
203 
204  pos = line.find('=');
205  if (pos == std::string::npos)
206  continue;
207 
208  std::string var =
209  boost::algorithm::trim_copy(line.substr(pos + 1));
210  std::string value =
211  boost::algorithm::trim_copy(line.substr(0, pos));
212 
213  params[var] = value;
214  }
215 
216  return params;
217 }
218 
219 template<typename T>
221  const std::map<std::string, std::string> &params,
222  T &param, const std::string &name)
223 {
224  if (param < 0){
225  param = getParameter(params, name, param);
226  }
227  if (param < 0)
228  throw cms::Exception("Generator|PartonShowerVeto")
229  << "The MGParamCMS header does not specify the jet "
230  "matching parameter \"" << name << "\", but it "
231  "is requested by the CMSSW configuration."
232  << std::endl;
233 }
234 
235 // implements the Madgraph method - use ME2pythia.f
236 
238 {
239  // read MadGraph run card
240 
241  std::map<std::string, std::string> parameters;
242 
243  std::vector<std::string> header = runInfo->findHeader("MGRunCard");
244  if (header.empty())
245  throw cms::Exception("Generator|PartonShowerVeto")
246  << "In order to use MadGraph jet matching, "
247  "the input file has to contain the corresponding "
248  "MadGraph headers." << std::endl;
249 
250  mgParams = parseHeader(header);
251 
252  // set variables in common block
253 
254  std::vector<Param> params;
255  std::vector<Param> values;
256  for(std::map<std::string, std::string>::const_iterator iter =
257  mgParams.begin(); iter != mgParams.end(); ++iter) {
258  params.push_back(" " + iter->first);
259  values.push_back(iter->second);
260 
261  }
262 
263  // set MG matching parameters
264 
265  uppriv_.ickkw = getParameter<int>("ickkw", 0);
266  memain_.mektsc = getParameter<int>("ktscheme", 0);
267 
268  header = runInfo->findHeader("MGParamCMS");
269 
270  std::map<std::string, std::string> mgInfoCMS = parseHeader(header);
271 
272  for(std::map<std::string, std::string>::const_iterator iter =
273  mgInfoCMS.begin(); iter != mgInfoCMS.end(); ++iter) {
274  std::cout<<"mgInfoCMS: "<<iter->first<<" "<<iter->second<<std::endl;
275 
276  }
277 
278 
279  updateOrDie(mgInfoCMS, memain_.etaclmax, "etaclmax");
280  updateOrDie(mgInfoCMS, memain_.qcut, "qcut");
281  updateOrDie(mgInfoCMS, memain_.minjets, "minjets");
282  updateOrDie(mgInfoCMS, memain_.maxjets, "maxjets");
283  updateOrDie(mgInfoCMS, memain_.showerkt, "showerkt");
284  updateOrDie(mgInfoCMS, memain_.nqmatch, "nqmatch");
285 
286  // run Fortran initialization code
287 
288  int nparam = params.size();
289  mginit_(&nparam, &params.front(), &values.front());
290  runInitialized = true;
291 }
292 
293 //void JetMatchingMadgraph::beforeHadronisation(
294 // const boost::shared_ptr<lhef::LHEEvent> &event)
295 
297 {
298  if (!runInitialized)
299  throw cms::Exception("Generator|PartonShowerVeto")
300  << "Run not initialized in JetMatchingMadgraph"
301  << std::endl;
302 
303 
304  if (uppriv_.ickkw)
305  {
306  std::vector<std::string> comments = event->getComments();
307  if (comments.size() == 1)
308  {
309  std::istringstream ss(comments[0].substr(1));
310  for(int i = 0; i < 1000; i++)
311  {
312  double pt;
313  ss >> pt;
314  if (!ss.good())
315  break;
316  pypart_.ptpart[i] = pt;
317  }
318  }
319  else
320  {
321  edm::LogWarning("Generator|LHEInterface")
322  << "Expected exactly one comment line per "
323  "event containing MadGraph parton scale "
324  "information."
325  << std::endl;
326 
327  const lhef::HEPEUP *hepeup = event->getHEPEUP();
328  for(int i = 2; i < hepeup->NUP; i++)
329  {
330  double mt2 =
331  hepeup->PUP[i][0] * hepeup->PUP[i][0] +
332  hepeup->PUP[i][1] * hepeup->PUP[i][1] +
333  hepeup->PUP[i][4] * hepeup->PUP[i][4];
334  pypart_.ptpart[i - 2] = std::sqrt(mt2);
335  }
336  }
337  }
338 
339  // mgevnt_();
340  eventInitialized = true;
341 }
342 
344 {
345  mgevnt_();
346  eventInitialized = true;
347  return;
348 }
349 
350 /*
351 int JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
352  const HepMC::GenEvent *finalState,
353  bool showeredFinalState)
354 */
355 int JetMatchingMadgraph::match( const lhef::LHEEvent* partonLevel, const std::vector<fastjet::PseudoJet>* jetInput )
356 {
357 /*
358  if (!showeredFinalState)
359  throw cms::Exception("Generator|LHEInterface")
360  << "MadGraph matching expected parton shower "
361  "final state." << std::endl;
362 */
363 
364  if (!runInitialized)
365  throw cms::Exception("Generator|LHEInterface")
366  << "Run not initialized in JetMatchingMadgraph"
367  << std::endl;
368 
369  if (!eventInitialized)
370  throw cms::Exception("Generator|LHEInterface")
371  << "Event not initialized in JetMatchingMadgraph"
372  << std::endl;
373 
374  if (soup)
376  else
378 
379  int veto = 0;
380  mgveto_(&veto);
381  fMatchingStatus=true;
382  eventInitialized = false;
383 
384  return veto;
385 }
386 
387 } // end namespace gen
T getParameter(std::string const &) const
struct gen::OUTTREE outtree_
struct gen::MEMAIN memain_
int i
Definition: DBlmapReader.cc:9
struct gen::MEMAEV memaev_
virtual void init(const lhef::LHERunInfo *runInfo)
struct gen::PYPART pypart_
double ptpart[1000]
virtual void beforeHadronisation(const lhef::LHEEvent *event)
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())
T sqrt(T t)
Definition: SSEVec.h:18
std::vector< FiveVector > PUP
Definition: LesHouches.h:248
tuple result
Definition: query.py:137
virtual int match(const lhef::LHEEvent *partonLevel, const std::vector< fastjet::PseudoJet > *jetInput)
T min(T a, T b)
Definition: MathUtil.h:58
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)
virtual void beforeHadronisationExec()
tuple cout
Definition: gather_cfg.py:145
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:523
volatile std::atomic< bool > shutdown_flag false
long double T
virtual double getJetEtaMax() const
struct gen::UPPRIV uppriv_