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 <string>
8 #include <cctype>
9 #include <map>
10 #include <set>
11 
12 #include <boost/shared_ptr.hpp>
13 #include <boost/algorithm/string/trim.hpp>
14 
15 #include <HepMC/GenEvent.h>
16 
19 
23 
24 namespace lhef {
25 
26 extern "C" {
27  #define PARAMLEN 20
28  namespace {
29  struct Param {
30  Param(const std::string &str)
31  {
32  int len = std::min(PARAMLEN,
33  (int)str.length());
34  std::memcpy(value, str.c_str(), len);
35  std::memset(value + len, ' ', PARAMLEN - len);
36  }
37 
38  char value[PARAMLEN];
39  };
40  }
41  extern void mginit_(int *npara, Param *params, Param *values);
42  extern void mgevnt_(void);
43  extern void mgveto_(int *veto);
44 
45  extern struct UPPRIV {
46  int lnhin, lnhout;
47  int mscal, ievnt;
48  int ickkw, iscale;
49  } uppriv_;
50 
51  extern struct MEMAIN {
54  int nexcres, excres[30];
55  } memain_;
56 
57  extern struct MEMAEV {
58  double ptclus[20];
59  int nljets, iexc, ifile;
60  } memaev_;
61 
62  extern struct PYPART {
63  int npart, npartd, ipart[1000];
64  double ptpart[1000];
65  } pypart_;
66 } // extern "C"
67 
69  public:
72 
73  private:
74  void init(const boost::shared_ptr<LHERunInfo> &runInfo);
75  void beforeHadronisation(const boost::shared_ptr<LHEEvent> &event);
76 
77  double match(const HepMC::GenEvent *partonLevel,
78  const HepMC::GenEvent *finalState,
79  bool showeredFinalState);
80 
81  std::set<std::string> capabilities() const;
82 
83  template<typename T>
84  static T parseParameter(const std::string &value);
85  template<typename T>
86  T getParameter(const std::string &var, const T &defValue = T()) const;
87 
88  std::map<std::string, std::string> mgParams;
89 
92  bool soup;
93  bool exclusive;
94 };
95 
96 template<typename T>
98 {
99  std::istringstream ss(value);
100  T result;
101  ss >> result;
102  return result;
103 }
104 
105 template<>
106 std::string JetMatchingMadgraph::parseParameter(const std::string &value)
107 {
108  std::string result;
109  if (!result.empty() && result[0] == '\'')
110  result = result.substr(1);
111  if (!result.empty() && result[result.length() - 1] == '\'')
112  result.resize(result.length() - 1);
113  return result;
114 }
115 
116 template<>
117 bool JetMatchingMadgraph::parseParameter(const std::string &value_)
118 {
119  std::string value(value_);
120  std::transform(value.begin(), value.end(),
121  value.begin(), (int(*)(int))std::toupper);
122  return value == "T" || value == "Y" ||
123  value == "1" || value == ".TRUE.";
124 }
125 
126 template<typename T>
127 T JetMatchingMadgraph::getParameter(const std::string &var,
128  const T &defValue) const
129 {
130  std::map<std::string, std::string>::const_iterator pos =
131  mgParams.find(var);
132  if (pos == mgParams.end())
133  return defValue;
134  return parseParameter<T>(pos->second);
135 }
136 
137 } // namespace lhef
138 
139 using namespace lhef;
140 
142  JetMatching(params),
143  runInitialized(false)
144 {
145  std::string mode = params.getParameter<std::string>("mode");
146  if (mode == "inclusive") {
147  soup = false;
148  exclusive = false;
149  } else if (mode == "exclusive") {
150  soup = false;
151  exclusive = true;
152  } else if (mode == "auto")
153  soup = true;
154  else
155  throw cms::Exception("Generator|LHEInterface")
156  << "Madgraph jet matching scheme requires \"mode\" "
157  "parameter to be set to either \"inclusive\", "
158  "\"exclusive\" or \"auto\"." << std::endl;
159 
160  memain_.etcjet = 0.;
161  memain_.rclmax = 0.0;
162  memain_.clfact = 0.0;
163  memain_.iexcfile = 0;
164  memain_.ktsche = 0;
165  memain_.etaclmax = params.getParameter<double>("etaclmax");
166  memain_.qcut = params.getParameter<double>("qcut");
167  memain_.minjets = params.getParameter<int>("minjets");
168  memain_.maxjets = params.getParameter<int>("maxjets");
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 // implements the Madgraph method - use ME2pythia.f
185 
186 void JetMatchingMadgraph::init(const boost::shared_ptr<LHERunInfo> &runInfo)
187 {
188  // read MadGraph run card
189 
190  std::map<std::string, std::string> parameters;
191 
192  std::vector<std::string> header = runInfo->findHeader("MGRunCard");
193  if (header.empty())
194  throw cms::Exception("Generator|LHEInterface")
195  << "In order to use MadGraph jet matching, "
196  "the input file has to contain the corresponding "
197  "MadGraph headers." << std::endl;
198 
199  mgParams.clear();
200  for(std::vector<std::string>::const_iterator iter = header.begin();
201  iter != header.end(); ++iter) {
202  std::string line = *iter;
203  if (line.empty() || line[0] == '#')
204  continue;
205 
206  std::string::size_type pos = line.find('!');
207  if (pos != std::string::npos)
208  line.resize(pos);
209 
210  pos = line.find('=');
211  if (pos == std::string::npos)
212  continue;
213 
214  std::string var =
215  boost::algorithm::trim_copy(line.substr(pos + 1));
216  std::string value =
217  boost::algorithm::trim_copy(line.substr(0, pos));
218 
219  mgParams[var] = value;
220  }
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 =
227  mgParams.begin(); iter != mgParams.end(); ++iter) {
228  params.push_back(" " + iter->first);
229  values.push_back(iter->second);
230 
231  }
232 
233  // set MG matching parameters
234 
235  uppriv_.ickkw = getParameter<int>("ickkw", 0);
236 
237  // run Fortran initialization code
238 
239  int nparam = params.size();
240  mginit_(&nparam, &params.front(), &values.front());
241  runInitialized = true;
242 }
243 
245  const boost::shared_ptr<LHEEvent> &event)
246 {
247  if (!runInitialized)
248  throw cms::Exception("Generator|LHEInterface")
249  << "Run not initialized in JetMatchingMadgraph"
250  << std::endl;
251 
252  if (uppriv_.ickkw) {
253  std::vector<std::string> comments = event->getComments();
254  if (comments.size() == 1) {
255  std::istringstream ss(comments[0].substr(1));
256  for(int i = 0; i < 1000; i++) {
257  double pt;
258  ss >> pt;
259  if (!ss.good())
260  break;
261  pypart_.ptpart[i] = pt;
262  }
263  } else {
264  edm::LogWarning("Generator|LHEInterface")
265  << "Expected exactly one comment line per "
266  "event containing MadGraph parton scale "
267  "information."
268  << std::endl;
269 
270  const HEPEUP *hepeup = event->getHEPEUP();
271  for(int i = 2; i < hepeup->NUP; i++) {
272  double mt2 =
273  hepeup->PUP[i][0] * hepeup->PUP[i][0] +
274  hepeup->PUP[i][1] * hepeup->PUP[i][1] +
275  hepeup->PUP[i][4] * hepeup->PUP[i][4];
276  pypart_.ptpart[i - 2] = std::sqrt(mt2);
277  }
278  }
279  }
280 
281  mgevnt_();
282  eventInitialized = true;
283 }
284 
285 double JetMatchingMadgraph::match(const HepMC::GenEvent *partonLevel,
286  const HepMC::GenEvent *finalState,
287  bool showeredFinalState)
288 {
289  if (!showeredFinalState)
290  throw cms::Exception("Generator|LHEInterface")
291  << "MadGraph matching expected parton shower "
292  "final state." << std::endl;
293 
294  if (!runInitialized)
295  throw cms::Exception("Generator|LHEInterface")
296  << "Run not initialized in JetMatchingMadgraph"
297  << std::endl;
298 
299  if (!eventInitialized)
300  throw cms::Exception("Generator|LHEInterface")
301  << "Event not initialized in JetMatchingMadgraph"
302  << std::endl;
303 
304  if (soup)
306  else
308 
309  int veto = 0;
310  mgveto_(&veto);
311  eventInitialized = false;
312 
313  return veto ? 0.0 : 1.0;
314 }
315 
struct lhef::MEMAEV memaev_
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
struct lhef::UPPRIV uppriv_
void mgevnt_(void)
#define PARAMLEN
#define min(a, b)
Definition: mlp_lapack.h:161
static T parseParameter(const std::string &value)
uint16_t size_type
void beforeHadronisation(const boost::shared_ptr< LHEEvent > &event)
void mginit_(int *npara, Param *params, Param *values)
struct lhef::PYPART pypart_
T sqrt(T t)
Definition: SSEVec.h:46
std::vector< FiveVector > PUP
Definition: LesHouches.h:248
tuple result
Definition: query.py:137
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
struct lhef::MEMAIN memain_
JetMatchingMadgraph(const edm::ParameterSet &params)
double match(const HepMC::GenEvent *partonLevel, const HepMC::GenEvent *finalState, bool showeredFinalState)
T getParameter(const std::string &var, const T &defValue=T()) const
#define DEFINE_LHE_JETMATCHING_PLUGIN(T)
Definition: JetMatching.h:79
double ptpart[1000]
std::set< std::string > capabilities() const
void mgveto_(int *veto)
std::map< std::string, std::string > mgParams
long double T
void init(const boost::shared_ptr< LHERunInfo > &runInfo)