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
gen::UPPRIV::ievnt
int ievnt
Definition: JetMatchingMadgraph.cc:45
gen::JetMatchingMadgraph::parseParameter
static T parseParameter(const std::string &value)
Definition: JetMatchingMadgraph.cc:71
mps_fire.i
i
Definition: mps_fire.py:355
MessageLogger.h
funct::false
false
Definition: Factorize.h:34
gen::JetMatchingMadgraph::~JetMatchingMadgraph
~JetMatchingMadgraph() override
Definition: JetMatchingMadgraph.cc:151
gen::JetMatchingMadgraph::updateOrDie
static void updateOrDie(const std::map< std::string, std::string > &params, T &param, const std::string &name)
Definition: JetMatchingMadgraph.cc:189
JetMatchingMadgraph.h
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
min
T min(T a, T b)
Definition: MathUtil.h:58
gen::JetMatchingMadgraph::match
int match(const lhef::LHEEvent *partonLevel, const std::vector< fastjet::PseudoJet > *jetInput) override
Definition: JetMatchingMadgraph.cc:304
gen::JetMatchingMadgraph::beforeHadronisation
void beforeHadronisation(const lhef::LHEEvent *event) override
Definition: JetMatchingMadgraph.cc:259
gather_cfg.cout
cout
Definition: gather_cfg.py:144
pos
Definition: PixelAliasList.h:18
gen::outtree_
struct gen::OUTTREE outtree_
ALCARECOPromptCalibProdSiPixelAli0T_cff.mode
mode
Definition: ALCARECOPromptCalibProdSiPixelAli0T_cff.py:96
gen::UPPRIV::lnhin
int lnhin
Definition: JetMatchingMadgraph.cc:44
gen::MEMAIN::excproc
int excproc
Definition: JetMatchingMadgraph.cc:53
gen::MEMAIN::iexcval
int iexcval[1000]
Definition: JetMatchingMadgraph.cc:53
gen::MEMAEV::iexc
int iexc
Definition: JetMatchingMadgraph.cc:61
gen::MEMAIN
Definition: JetMatchingMadgraph.cc:49
gen::JetMatchingMadgraph::JetMatchingMadgraph
JetMatchingMadgraph(const edm::ParameterSet &params)
Definition: JetMatchingMadgraph.cc:110
gen::JetMatchingMadgraph::beforeHadronisationExec
void beforeHadronisationExec() override
Definition: JetMatchingMadgraph.cc:293
gen::JetMatchingMadgraph::mgParams
std::map< std::string, std::string > mgParams
Definition: JetMatchingMadgraph.h:41
gen::MEMAIN::rclmax
double rclmax
Definition: JetMatchingMadgraph.cc:50
gen::MEMAIN::mektsc
int mektsc
Definition: JetMatchingMadgraph.cc:52
gen::MEMAIN::etcjet
double etcjet
Definition: JetMatchingMadgraph.cc:50
gen::JetMatchingMadgraph::getParameter
static T getParameter(const std::map< std::string, std::string > &params, const std::string &var, const T &defValue=T())
Definition: JetMatchingMadgraph.cc:96
gen::JetMatchingMadgraph::init
void init(const lhef::LHERunInfo *runInfo) override
Definition: JetMatchingMadgraph.cc:206
gen::JetMatching
Definition: JetMatching.h:27
gen::MEMAEV
Definition: JetMatchingMadgraph.cc:59
gen::JetMatchingMadgraph::runInitialized
bool runInitialized
Definition: JetMatchingMadgraph.h:43
parameters
parameters
Definition: BeamSpot_PayloadInspector.cc:14
gen::UPPRIV::lnhout
int lnhout
Definition: JetMatchingMadgraph.cc:44
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
gen::MEMAIN::nqmatch
int nqmatch
Definition: JetMatchingMadgraph.cc:53
gen::MEMAIN::ktsche
int ktsche
Definition: JetMatchingMadgraph.cc:51
gen::MEMAIN::clfact
double clfact
Definition: JetMatchingMadgraph.cc:50
lhef::HEPEUP::NUP
int NUP
Definition: LesHouches.h:184
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
gen::MEMAIN::qcut
double qcut
Definition: JetMatchingMadgraph.cc:50
gen::MEMAIN::maxjets
int maxjets
Definition: JetMatchingMadgraph.cc:51
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
str
#define str(s)
Definition: TestProcessor.cc:48
gen::mgveto_
void mgveto_(int *veto)
HcalDetIdTransform::transform
unsigned transform(const HcalDetId &id, unsigned transformCode)
Definition: HcalDetIdTransform.cc:7
gen
Definition: PythiaDecays.h:13
gen::memain_
struct gen::MEMAIN memain_
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
gen::parseHeader
static std::map< std::string, std::string > parseHeader(const std::vector< std::string > &header)
Definition: JetMatchingMadgraph.cc:163
edm::LogWarning
Definition: MessageLogger.h:141
indexGen.comments
comments
Definition: indexGen.py:75
MT2Analyzer.mt2
mt2
Definition: MT2Analyzer.py:251
lhef::LHERunInfo
Definition: LHERunInfo.h:25
edm::ParameterSet
Definition: ParameterSet.h:36
gen::JetMatchingMadgraph::capabilities
std::set< std::string > capabilities() const override
Definition: JetMatchingMadgraph.cc:155
gen::PYPART::npartd
int npartd
Definition: JetMatchingMadgraph.cc:65
gen::MEMAIN::minjets
int minjets
Definition: JetMatchingMadgraph.cc:51
LHERunInfo.h
lhef::LHEEvent
Definition: LHEEvent.h:23
gen::UPPRIV
Definition: JetMatchingMadgraph.cc:43
gen::OUTTREE
Definition: JetMatchingMadgraph.cc:57
createfilelist.int
int
Definition: createfilelist.py:10
gen::PYPART::npart
int npart
Definition: JetMatchingMadgraph.cc:65
gen::MEMAIN::etaclmax
double etaclmax
Definition: JetMatchingMadgraph.cc:50
value
Definition: value.py:1
gen::PYPART::ptpart
double ptpart[1000]
Definition: JetMatchingMadgraph.cc:66
gen::MEMAEV::ifile
int ifile
Definition: JetMatchingMadgraph.cc:61
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
gen::JetMatchingMadgraph::exclusive
bool exclusive
Definition: JetMatchingMadgraph.h:46
gen::JetMatching::fMatchingStatus
bool fMatchingStatus
Definition: JetMatching.h:86
gen::mginit_
void mginit_(int *npara, Param *params, Param *values)
lhef::HEPEUP
Definition: LesHouches.h:138
gen::mgevnt_
void mgevnt_(void)
gen::MEMAEV::ptclus
double ptclus[20]
Definition: JetMatchingMadgraph.cc:60
gen::JetMatchingMadgraph::getJetEtaMax
double getJetEtaMax() const override
Definition: JetMatchingMadgraph.cc:153
gen::uppriv_
struct gen::UPPRIV uppriv_
gen::PYPART
Definition: JetMatchingMadgraph.cc:64
gen::MEMAIN::showerkt
double showerkt
Definition: JetMatchingMadgraph.cc:50
T
long double T
Definition: Basic3DVectorLD.h:48
gen::memaev_
struct gen::MEMAEV memaev_
gen::pypart_
struct gen::PYPART pypart_
relativeConstraints.value
value
Definition: relativeConstraints.py:53
gen::MEMAIN::excres
int excres[30]
Definition: JetMatchingMadgraph.cc:52
Exception
Definition: hltDiff.cc:246
gen::MEMAIN::nexcres
int nexcres
Definition: JetMatchingMadgraph.cc:52
gen::JetMatchingMadgraph::soup
bool soup
Definition: JetMatchingMadgraph.h:45
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
gen::PYPART::ipart
int ipart[1000]
Definition: JetMatchingMadgraph.cc:65
LHEEvent.h
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
gen::MEMAIN::nosingrad
bool nosingrad
Definition: JetMatchingMadgraph.cc:54
gen::UPPRIV::iscale
int iscale
Definition: JetMatchingMadgraph.cc:46
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
gen::MEMAEV::nljets
int nljets
Definition: JetMatchingMadgraph.cc:61
gen::MEMAIN::iexcfile
int iexcfile
Definition: JetMatchingMadgraph.cc:51
mps_fire.result
result
Definition: mps_fire.py:303
cms::Exception
Definition: Exception.h:70
gen::UPPRIV::mscal
int mscal
Definition: JetMatchingMadgraph.cc:45
ParameterSet.h
gen::JetMatchingMadgraph::eventInitialized
bool eventInitialized
Definition: JetMatchingMadgraph.h:44
gen::UPPRIV::ickkw
int ickkw
Definition: JetMatchingMadgraph.cc:46
gen::MEMAIN::jetprocs
bool jetprocs
Definition: JetMatchingMadgraph.cc:54
event
Definition: event.py:1
mps_splice.line
line
Definition: mps_splice.py:76
lhef::LHERunInfo::findHeader
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:436
PbPb_ZMuSkimMuonDPG_cff.veto
veto
Definition: PbPb_ZMuSkimMuonDPG_cff.py:61
PARAMLEN
#define PARAMLEN
Definition: JetMatchingMadgraph.cc:27
lhef::HEPEUP::PUP
std::vector< FiveVector > PUP
Definition: LesHouches.h:246
gen::OUTTREE::flag
int flag
Definition: JetMatchingMadgraph.cc:57
gen::MEMAIN::iexcproc
int iexcproc[1000]
Definition: JetMatchingMadgraph.cc:53