CMS 3D CMS Logo

GenWeightsTableProducer.cc
Go to the documentation of this file.
15 #include "boost/algorithm/string.hpp"
16 
17 #include <vector>
18 #include <unordered_map>
19 #include <iostream>
20 #include <regex>
21 
22 namespace {
24  struct Counter {
25  Counter() : num(0), sumw(0), sumw2(0), sumPDF(), sumScale(), sumRwgt(), sumNamed(), sumPS() {}
26 
27  // the counters
28  long long num;
29  long double sumw;
30  long double sumw2;
31  std::vector<long double> sumPDF, sumScale, sumRwgt, sumNamed, sumPS;
32 
33  void clear() {
34  num = 0;
35  sumw = 0;
36  sumw2 = 0;
37  sumPDF.clear();
38  sumScale.clear();
39  sumRwgt.clear();
40  sumNamed.clear(), sumPS.clear();
41  }
42 
43  // inc the counters
44  void incGenOnly(double w) {
45  num++;
46  sumw += w;
47  sumw2 += (w * w);
48  }
49 
50  void incPSOnly(double w0, const std::vector<double>& wPS) {
51  if (!wPS.empty()) {
52  if (sumPS.empty())
53  sumPS.resize(wPS.size(), 0);
54  for (unsigned int i = 0, n = wPS.size(); i < n; ++i)
55  sumPS[i] += (w0 * wPS[i]);
56  }
57  }
58 
59  void incLHE(double w0,
60  const std::vector<double>& wScale,
61  const std::vector<double>& wPDF,
62  const std::vector<double>& wRwgt,
63  const std::vector<double>& wNamed,
64  const std::vector<double>& wPS) {
65  // add up weights
66  incGenOnly(w0);
67  // then add up variations
68  if (!wScale.empty()) {
69  if (sumScale.empty())
70  sumScale.resize(wScale.size(), 0);
71  for (unsigned int i = 0, n = wScale.size(); i < n; ++i)
72  sumScale[i] += (w0 * wScale[i]);
73  }
74  if (!wPDF.empty()) {
75  if (sumPDF.empty())
76  sumPDF.resize(wPDF.size(), 0);
77  for (unsigned int i = 0, n = wPDF.size(); i < n; ++i)
78  sumPDF[i] += (w0 * wPDF[i]);
79  }
80  if (!wRwgt.empty()) {
81  if (sumRwgt.empty())
82  sumRwgt.resize(wRwgt.size(), 0);
83  for (unsigned int i = 0, n = wRwgt.size(); i < n; ++i)
84  sumRwgt[i] += (w0 * wRwgt[i]);
85  }
86  if (!wNamed.empty()) {
87  if (sumNamed.empty())
88  sumNamed.resize(wNamed.size(), 0);
89  for (unsigned int i = 0, n = wNamed.size(); i < n; ++i)
90  sumNamed[i] += (w0 * wNamed[i]);
91  }
92  incPSOnly(w0, wPS);
93  }
94 
95  void merge(const Counter& other) {
96  num += other.num;
97  sumw += other.sumw;
98  sumw2 += other.sumw2;
99  if (sumScale.empty() && !other.sumScale.empty())
100  sumScale.resize(other.sumScale.size(), 0);
101  if (sumPDF.empty() && !other.sumPDF.empty())
102  sumPDF.resize(other.sumPDF.size(), 0);
103  if (sumRwgt.empty() && !other.sumRwgt.empty())
104  sumRwgt.resize(other.sumRwgt.size(), 0);
105  if (sumNamed.empty() && !other.sumNamed.empty())
106  sumNamed.resize(other.sumNamed.size(), 0);
107  if (sumPS.empty() && !other.sumPS.empty())
108  sumPS.resize(other.sumPS.size(), 0);
109  if (!other.sumScale.empty())
110  for (unsigned int i = 0, n = sumScale.size(); i < n; ++i)
111  sumScale[i] += other.sumScale[i];
112  if (!other.sumPDF.empty())
113  for (unsigned int i = 0, n = sumPDF.size(); i < n; ++i)
114  sumPDF[i] += other.sumPDF[i];
115  if (!other.sumRwgt.empty())
116  for (unsigned int i = 0, n = sumRwgt.size(); i < n; ++i)
117  sumRwgt[i] += other.sumRwgt[i];
118  if (!other.sumNamed.empty())
119  for (unsigned int i = 0, n = sumNamed.size(); i < n; ++i)
120  sumNamed[i] += other.sumNamed[i];
121  if (!other.sumPS.empty())
122  for (unsigned int i = 0, n = sumPS.size(); i < n; ++i)
123  sumPS[i] += other.sumPS[i];
124  }
125  };
126 
127  struct CounterMap {
128  std::map<std::string, Counter> countermap;
129  Counter* active_el = nullptr;
130  std::string active_label = "";
131  void merge(const CounterMap& other) {
132  for (const auto& y : other.countermap)
133  countermap[y.first].merge(y.second);
134  active_el = nullptr;
135  }
136  void clear() {
137  for (auto x : countermap)
138  x.second.clear();
139  }
140  void setLabel(std::string label) {
141  active_el = &(countermap[label]);
142  active_label = label;
143  }
144  void checkLabelSet() {
145  if (!active_el)
146  throw cms::Exception("LogicError", "Called CounterMap::get() before setting the active label\n");
147  }
148  Counter* get() {
149  checkLabelSet();
150  return active_el;
151  }
152  std::string& getLabel() {
153  checkLabelSet();
154  return active_label;
155  }
156  };
157 
159  struct DynamicWeightChoice {
160  // choice of LHE weights
161  // ---- scale ----
162  std::vector<std::string> scaleWeightIDs;
163  std::string scaleWeightsDoc;
164  // ---- pdf ----
165  std::vector<std::string> pdfWeightIDs;
166  std::string pdfWeightsDoc;
167  // ---- rwgt ----
168  std::vector<std::string> rwgtIDs;
169  std::string rwgtWeightDoc;
170  };
171 
172  float stof_fortrancomp(const std::string& str) {
173  std::string::size_type match = str.find("d");
174  if (match != std::string::npos) {
175  std::string pre = str.substr(0, match);
176  std::string post = str.substr(match + 1);
177  return std::stof(pre) * std::pow(10.0f, std::stof(post));
178  } else {
179  return std::stof(str);
180  }
181  }
183  struct ScaleVarWeight {
184  std::string wid, label;
185  std::pair<float, float> scales;
186  ScaleVarWeight(const std::string& id, const std::string& text, const std::string& muR, const std::string& muF)
187  : wid(id), label(text), scales(stof_fortrancomp(muR), stof_fortrancomp(muF)) {}
188  bool operator<(const ScaleVarWeight& other) {
189  return (scales == other.scales ? wid < other.wid : scales < other.scales);
190  }
191  };
192  struct PDFSetWeights {
193  std::vector<std::string> wids;
194  std::pair<unsigned int, unsigned int> lhaIDs;
195  PDFSetWeights(const std::string& wid, unsigned int lhaID) : wids(1, wid), lhaIDs(lhaID, lhaID) {}
196  bool operator<(const PDFSetWeights& other) const { return lhaIDs < other.lhaIDs; }
197  void add(const std::string& wid, unsigned int lhaID) {
198  wids.push_back(wid);
199  lhaIDs.second = lhaID;
200  }
201  bool maybe_add(const std::string& wid, unsigned int lhaID) {
202  if (lhaID == lhaIDs.second + 1) {
203  lhaIDs.second++;
204  wids.push_back(wid);
205  return true;
206  } else {
207  return false;
208  }
209  }
210  };
211 } // namespace
212 
213 class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<CounterMap>,
214  edm::RunCache<DynamicWeightChoice>,
215  edm::RunSummaryCache<CounterMap>,
216  edm::EndRunProducer> {
217 public:
219  : genTag_(consumes<GenEventInfoProduct>(params.getParameter<edm::InputTag>("genEvent"))),
220  lheLabel_(params.getParameter<std::vector<edm::InputTag>>("lheInfo")),
221  lheTag_(edm::vector_transform(lheLabel_,
222  [this](const edm::InputTag& tag) { return mayConsume<LHEEventProduct>(tag); })),
223  lheRunTag_(edm::vector_transform(
224  lheLabel_, [this](const edm::InputTag& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
225  genLumiInfoHeadTag_(
226  mayConsume<GenLumiInfoHeader, edm::InLumi>(params.getParameter<edm::InputTag>("genLumiInfoHeader"))),
227  namedWeightIDs_(params.getParameter<std::vector<std::string>>("namedWeightIDs")),
228  namedWeightLabels_(params.getParameter<std::vector<std::string>>("namedWeightLabels")),
229  lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")),
230  maxPdfWeights_(params.getParameter<uint32_t>("maxPdfWeights")),
231  debug_(params.getUntrackedParameter<bool>("debug", false)),
232  debugRun_(debug_.load()),
233  hasIssuedWarning_(false) {
234  produces<nanoaod::FlatTable>();
235  produces<std::string>("genModel");
236  produces<nanoaod::FlatTable>("LHEScale");
237  produces<nanoaod::FlatTable>("LHEPdf");
238  produces<nanoaod::FlatTable>("LHEReweighting");
239  produces<nanoaod::FlatTable>("LHENamed");
240  produces<nanoaod::FlatTable>("PS");
241  produces<nanoaod::MergeableCounterTable, edm::Transition::EndRun>();
242  if (namedWeightIDs_.size() != namedWeightLabels_.size()) {
243  throw cms::Exception("Configuration", "Size mismatch between namedWeightIDs & namedWeightLabels");
244  }
245  for (const edm::ParameterSet& pdfps : params.getParameter<std::vector<edm::ParameterSet>>("preferredPDFs")) {
246  const std::string& name = pdfps.getParameter<std::string>("name");
247  uint32_t lhaid = pdfps.getParameter<uint32_t>("lhaid");
248  preferredPDFLHAIDs_.push_back(lhaid);
249  lhaNameToID_[name] = lhaid;
250  lhaNameToID_[name + ".LHgrid"] = lhaid;
251  }
252  }
253 
255 
256  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
257  // get my counter for weights
258  Counter* counter = streamCache(id)->get();
259 
260  // generator information (always available)
262  iEvent.getByToken(genTag_, genInfo);
263  double weight = genInfo->weight();
264 
265  // table for gen info, always available
266  auto out = std::make_unique<nanoaod::FlatTable>(1, "genWeight", true);
267  out->setDoc("generator weight");
268  out->addColumnValue<float>("", weight, "generator weight", nanoaod::FlatTable::FloatColumn);
269  iEvent.put(std::move(out));
270 
271  std::string model_label = streamCache(id)->getLabel();
272  auto outM = std::make_unique<std::string>((!model_label.empty()) ? std::string("GenModel_") + model_label : "");
273  iEvent.put(std::move(outM), "genModel");
274 
275  // tables for LHE weights, may not be filled
276  std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
277  std::unique_ptr<nanoaod::FlatTable> genPSTab;
278 
280  for (const auto& lheTag : lheTag_) {
281  iEvent.getByToken(lheTag, lheInfo);
282  if (lheInfo.isValid()) {
283  break;
284  }
285  }
286  if (lheInfo.isValid()) {
287  // get the dynamic choice of weights
288  const DynamicWeightChoice* weightChoice = runCache(iEvent.getRun().index());
289  // go fill tables
290  fillLHEWeightTables(
291  counter, weightChoice, weight, *lheInfo, *genInfo, lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab, genPSTab);
292  } else {
293  // Still try to add the PS weights
294  fillOnlyPSWeightTable(counter, weight, *genInfo, genPSTab);
295  // make dummy values
296  lheScaleTab.reset(new nanoaod::FlatTable(1, "LHEScaleWeights", true));
297  lhePdfTab.reset(new nanoaod::FlatTable(1, "LHEPdfWeights", true));
298  lheRwgtTab.reset(new nanoaod::FlatTable(1, "LHEReweightingWeights", true));
299  lheNamedTab.reset(new nanoaod::FlatTable(1, "LHENamedWeights", true));
300  if (!hasIssuedWarning_.exchange(true)) {
301  edm::LogWarning("LHETablesProducer") << "No LHEEventProduct, so there will be no LHE Tables\n";
302  }
303  }
304 
305  iEvent.put(std::move(lheScaleTab), "LHEScale");
306  iEvent.put(std::move(lhePdfTab), "LHEPdf");
307  iEvent.put(std::move(lheRwgtTab), "LHEReweighting");
308  iEvent.put(std::move(lheNamedTab), "LHENamed");
309  iEvent.put(std::move(genPSTab), "PS");
310  }
311 
313  const DynamicWeightChoice* weightChoice,
314  double genWeight,
315  const LHEEventProduct& lheProd,
316  const GenEventInfoProduct& genProd,
317  std::unique_ptr<nanoaod::FlatTable>& outScale,
318  std::unique_ptr<nanoaod::FlatTable>& outPdf,
319  std::unique_ptr<nanoaod::FlatTable>& outRwgt,
320  std::unique_ptr<nanoaod::FlatTable>& outNamed,
321  std::unique_ptr<nanoaod::FlatTable>& outPS) const {
322  bool lheDebug = debug_.exchange(
323  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
324 
325  const std::vector<std::string>& scaleWeightIDs = weightChoice->scaleWeightIDs;
326  const std::vector<std::string>& pdfWeightIDs = weightChoice->pdfWeightIDs;
327  const std::vector<std::string>& rwgtWeightIDs = weightChoice->rwgtIDs;
328 
329  double w0 = lheProd.originalXWGTUP();
330 
331  std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1),
332  wNamed(namedWeightIDs_.size(), 1);
333  for (auto& weight : lheProd.weights()) {
334  if (lheDebug)
335  printf("Weight %+9.5f rel %+9.5f for id %s\n", weight.wgt, weight.wgt / w0, weight.id.c_str());
336  // now we do it slowly, can be optimized
337  auto mScale = std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(), weight.id);
338  if (mScale != scaleWeightIDs.end())
339  wScale[mScale - scaleWeightIDs.begin()] = weight.wgt / w0;
340 
341  auto mPDF = std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(), weight.id);
342  if (mPDF != pdfWeightIDs.end())
343  wPDF[mPDF - pdfWeightIDs.begin()] = weight.wgt / w0;
344 
345  auto mRwgt = std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(), weight.id);
346  if (mRwgt != rwgtWeightIDs.end())
347  wRwgt[mRwgt - rwgtWeightIDs.begin()] = weight.wgt / w0;
348 
349  auto mNamed = std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(), weight.id);
350  if (mNamed != namedWeightIDs_.end())
351  wNamed[mNamed - namedWeightIDs_.begin()] = weight.wgt / w0;
352  }
353 
354  int vectorSize = (genProd.weights().size() == 14 || genProd.weights().size() == 46) ? 4 : 1;
355  std::vector<double> wPS(vectorSize, 1);
356  if (vectorSize > 1) {
357  for (unsigned int i = 6; i < 10; i++) {
358  wPS[i - 6] = (genProd.weights()[i]) / w0;
359  }
360  }
361  outPS.reset(new nanoaod::FlatTable(wPS.size(), "PSWeight", false));
362  outPS->addColumn<float>("",
363  wPS,
364  vectorSize > 1 ? "PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 "
365  "FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 "
366  : "dummy PS weight (1.0) ",
368  lheWeightPrecision_);
369 
370  outScale.reset(new nanoaod::FlatTable(wScale.size(), "LHEScaleWeight", false));
371  outScale->addColumn<float>(
372  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
373 
374  outPdf.reset(new nanoaod::FlatTable(wPDF.size(), "LHEPdfWeight", false));
375  outPdf->addColumn<float>(
376  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
377 
378  outRwgt.reset(new nanoaod::FlatTable(wRwgt.size(), "LHEReweightingWeight", false));
379  outRwgt->addColumn<float>(
380  "", wRwgt, weightChoice->rwgtWeightDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
381 
382  outNamed.reset(new nanoaod::FlatTable(1, "LHEWeight", true));
383  outNamed->addColumnValue<float>("originalXWGTUP",
384  lheProd.originalXWGTUP(),
385  "Nominal event weight in the LHE file",
387  for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
388  outNamed->addColumnValue<float>(namedWeightLabels_[i],
389  wNamed[i],
390  "LHE weight for id " + namedWeightIDs_[i] + ", relative to nominal",
392  lheWeightPrecision_);
393  }
394 
395  counter->incLHE(genWeight, wScale, wPDF, wRwgt, wNamed, wPS);
396  }
397 
399  double genWeight,
400  const GenEventInfoProduct& genProd,
401  std::unique_ptr<nanoaod::FlatTable>& outPS) const {
402  int vectorSize = (genProd.weights().size() == 14 || genProd.weights().size() == 46) ? 4 : 1;
403 
404  std::vector<double> wPS(vectorSize, 1);
405  if (vectorSize > 1) {
406  for (unsigned int i = 6; i < 10; i++) {
407  wPS[i - 6] = (genProd.weights()[i]) / genWeight;
408  }
409  }
410 
411  outPS.reset(new nanoaod::FlatTable(wPS.size(), "PSWeight", false));
412  outPS->addColumn<float>("",
413  wPS,
414  vectorSize > 1 ? "PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 "
415  "FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 "
416  : "dummy PS weight (1.0) ",
418  lheWeightPrecision_);
419 
420  counter->incGenOnly(genWeight);
421  counter->incPSOnly(genWeight, wPS);
422  }
423 
424  // create an empty counter
425  std::shared_ptr<DynamicWeightChoice> globalBeginRun(edm::Run const& iRun, edm::EventSetup const&) const override {
427 
428  bool lheDebug = debugRun_.exchange(
429  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
430  auto weightChoice = std::make_shared<DynamicWeightChoice>();
431 
432  // getByToken throws since we're not in the endRun (see https://github.com/cms-sw/cmssw/pull/18499)
433  //if (iRun.getByToken(lheRunTag_, lheInfo)) {
434  for (const auto& lheLabel : lheLabel_) {
435  iRun.getByLabel(lheLabel, lheInfo);
436  if (lheInfo.isValid()) {
437  break;
438  }
439  }
440  if (lheInfo.isValid()) {
441  std::vector<ScaleVarWeight> scaleVariationIDs;
442  std::vector<PDFSetWeights> pdfSetWeightIDs;
443  std::vector<std::string> lheReweighingIDs;
444 
445  std::regex weightgroupmg26x("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
446  std::regex weightgroup("<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
447  std::regex weightgroupRwgt("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
448  std::regex endweightgroup("</weightgroup>");
449  std::regex scalewmg26x(
450  "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"("
451  "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
452  std::regex scalew(
453  "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+("
454  "?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
455  std::regex pdfw(
456  "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
457  std::regex pdfwOld("<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
458  std::regex pdfwmg26x(
459  "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF "
460  "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
461  "weight>");
462  std::regex rwgt("<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
463  std::smatch groups;
464  for (auto iter = lheInfo->headers_begin(), end = lheInfo->headers_end(); iter != end; ++iter) {
465  if (iter->tag() != "initrwgt") {
466  if (lheDebug)
467  std::cout << "Skipping LHE header with tag" << iter->tag() << std::endl;
468  continue;
469  }
470  if (lheDebug)
471  std::cout << "Found LHE header with tag" << iter->tag() << std::endl;
472  std::vector<std::string> lines = iter->lines();
473  bool missed_weightgroup =
474  false; //Needed because in some of the samples ( produced with MG26X ) a small part of the header info is ordered incorrectly
475  bool ismg26x = false;
476  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
477  ++iLine) { //First start looping through the lines to see which weightgroup pattern is matched
478  boost::replace_all(lines[iLine], "&lt;", "<");
479  boost::replace_all(lines[iLine], "&gt;", ">");
480  if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
481  ismg26x = true;
482  }
483  }
484  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
485  if (lheDebug)
486  std::cout << lines[iLine];
487  if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
488  std::string groupname = groups.str(2);
489  if (ismg26x)
490  groupname = groups.str(1);
491  if (lheDebug)
492  std::cout << ">>> Looks like the beginning of a weight group for '" << groupname << "'" << std::endl;
493  if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation") {
494  if (lheDebug)
495  std::cout << ">>> Looks like scale variation for theory uncertainties" << std::endl;
496  for (++iLine; iLine < nLines; ++iLine) {
497  if (lheDebug)
498  std::cout << " " << lines[iLine];
499  if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
500  if (lheDebug)
501  std::cout << " >>> Scale weight " << groups[1].str() << " for " << groups[3].str() << " , "
502  << groups[4].str() << " , " << groups[5].str() << std::endl;
503  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
504  } else if (std::regex_search(lines[iLine], endweightgroup)) {
505  if (lheDebug)
506  std::cout << ">>> Looks like the end of a weight group" << std::endl;
507  if (!missed_weightgroup) {
508  break;
509  } else
510  missed_weightgroup = false;
511  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
512  if (lheDebug)
513  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
514  "of the group."
515  << std::endl;
516  if (ismg26x)
517  missed_weightgroup = true;
518  --iLine; // rewind by one, and go back to the outer loop
519  break;
520  }
521  }
522  } else if (groupname == "PDF_variation" || groupname.find("PDF_variation ") == 0) {
523  if (lheDebug)
524  std::cout << ">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
525  for (++iLine; iLine < nLines; ++iLine) {
526  if (lheDebug)
527  std::cout << " " << lines[iLine];
528  if (std::regex_search(lines[iLine], groups, pdfw)) {
529  unsigned int lhaID = std::stoi(groups.str(2));
530  if (lheDebug)
531  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
532  << std::endl;
533  if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
534  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
535  }
536  } else if (std::regex_search(lines[iLine], endweightgroup)) {
537  if (lheDebug)
538  std::cout << ">>> Looks like the end of a weight group" << std::endl;
539  if (!missed_weightgroup) {
540  break;
541  } else
542  missed_weightgroup = false;
543  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
544  if (lheDebug)
545  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
546  "of the group."
547  << std::endl;
548  if (ismg26x)
549  missed_weightgroup = true;
550  --iLine; // rewind by one, and go back to the outer loop
551  break;
552  }
553  }
554  } else if (groupname == "PDF_variation1" || groupname == "PDF_variation2") {
555  if (lheDebug)
556  std::cout << ">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
557  unsigned int lastid = 0;
558  for (++iLine; iLine < nLines; ++iLine) {
559  if (lheDebug)
560  std::cout << " " << lines[iLine];
561  if (std::regex_search(lines[iLine], groups, pdfw)) {
562  unsigned int id = std::stoi(groups.str(1));
563  unsigned int lhaID = std::stoi(groups.str(2));
564  if (lheDebug)
565  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
566  << std::endl;
567  if (id != (lastid + 1) || pdfSetWeightIDs.empty()) {
568  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
569  } else {
570  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
571  }
572  lastid = id;
573  } else if (std::regex_search(lines[iLine], endweightgroup)) {
574  if (lheDebug)
575  std::cout << ">>> Looks like the end of a weight group" << std::endl;
576  if (!missed_weightgroup) {
577  break;
578  } else
579  missed_weightgroup = false;
580  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
581  if (lheDebug)
582  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
583  "of the group."
584  << std::endl;
585  if (ismg26x)
586  missed_weightgroup = true;
587  --iLine; // rewind by one, and go back to the outer loop
588  break;
589  }
590  }
591  } else if (lhaNameToID_.find(groupname) != lhaNameToID_.end()) {
592  if (lheDebug)
593  std::cout << ">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
594  unsigned int firstLhaID = lhaNameToID_.find(groupname)->second;
595  bool first = true;
596  for (++iLine; iLine < nLines; ++iLine) {
597  if (lheDebug)
598  std::cout << " " << lines[iLine];
599  if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
600  unsigned int member = 0;
601  if (ismg26x == 0) {
602  member = std::stoi(groups.str(2));
603  } else {
604  if (!groups.str(4).empty()) {
605  member = std::stoi(groups.str(4));
606  }
607  }
608  unsigned int lhaID = member + firstLhaID;
609  if (lheDebug)
610  std::cout << " >>> PDF weight " << groups.str(1) << " for " << member << " = " << lhaID
611  << std::endl;
612  //if (member == 0) continue; // let's keep also the central value for now
613  if (first) {
614  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
615  first = false;
616  } else {
617  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
618  }
619  } else if (std::regex_search(lines[iLine], endweightgroup)) {
620  if (lheDebug)
621  std::cout << ">>> Looks like the end of a weight group" << std::endl;
622  if (!missed_weightgroup) {
623  break;
624  } else
625  missed_weightgroup = false;
626  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
627  if (lheDebug)
628  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
629  "of the group."
630  << std::endl;
631  if (ismg26x)
632  missed_weightgroup = true;
633  --iLine; // rewind by one, and go back to the outer loop
634  break;
635  }
636  }
637  } else {
638  for (++iLine; iLine < nLines; ++iLine) {
639  if (lheDebug)
640  std::cout << " " << lines[iLine];
641  if (std::regex_search(lines[iLine], groups, endweightgroup)) {
642  if (lheDebug)
643  std::cout << ">>> Looks like the end of a weight group" << std::endl;
644  if (!missed_weightgroup) {
645  break;
646  } else
647  missed_weightgroup = false;
648  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
649  if (lheDebug)
650  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
651  "of the group."
652  << std::endl;
653  if (ismg26x)
654  missed_weightgroup = true;
655  --iLine; // rewind by one, and go back to the outer loop
656  break;
657  }
658  }
659  }
660  } else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
661  std::string groupname = groups.str(1);
662  if (groupname == "mg_reweighting") {
663  if (lheDebug)
664  std::cout << ">>> Looks like a LHE weights for reweighting" << std::endl;
665  for (++iLine; iLine < nLines; ++iLine) {
666  if (lheDebug)
667  std::cout << " " << lines[iLine];
668  if (std::regex_search(lines[iLine], groups, rwgt)) {
669  std::string rwgtID = groups.str(1);
670  if (lheDebug)
671  std::cout << " >>> LHE reweighting weight: " << rwgtID << std::endl;
672  if (std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
673  // we're only interested in the beggining of the block
674  lheReweighingIDs.emplace_back(rwgtID);
675  }
676  } else if (std::regex_search(lines[iLine], endweightgroup)) {
677  if (lheDebug)
678  std::cout << ">>> Looks like the end of a weight group" << std::endl;
679  if (!missed_weightgroup) {
680  break;
681  } else
682  missed_weightgroup = false;
683  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
684  if (lheDebug)
685  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
686  "of the group."
687  << std::endl;
688  if (ismg26x)
689  missed_weightgroup = true;
690  --iLine; // rewind by one, and go back to the outer loop
691  break;
692  }
693  }
694  }
695  }
696  }
697  //std::cout << "============= END [ " << iter->tag() << " ] ============ \n\n" << std::endl;
698 
699  // ----- SCALE VARIATIONS -----
700  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
701  if (lheDebug)
702  std::cout << "Found " << scaleVariationIDs.size() << " scale variations: " << std::endl;
703  std::stringstream scaleDoc;
704  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
705  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
706  const auto& sw = scaleVariationIDs[isw];
707  if (isw)
708  scaleDoc << "; ";
709  scaleDoc << "[" << isw << "] is " << sw.label;
710  weightChoice->scaleWeightIDs.push_back(sw.wid);
711  if (lheDebug)
712  printf(" id %s: scales ren = % .2f fact = % .2f text = %s\n",
713  sw.wid.c_str(),
714  sw.scales.first,
715  sw.scales.second,
716  sw.label.c_str());
717  }
718  if (!scaleVariationIDs.empty())
719  weightChoice->scaleWeightsDoc = scaleDoc.str();
720 
721  // ------ PDF VARIATIONS (take the preferred one) -----
722  if (lheDebug) {
723  std::cout << "Found " << pdfSetWeightIDs.size() << " PDF set errors: " << std::endl;
724  for (const auto& pw : pdfSetWeightIDs)
725  printf("lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
726  pw.lhaIDs.first,
727  pw.lhaIDs.second,
728  pw.wids.size(),
729  pw.wids.front().c_str());
730  }
731 
732  // ------ LHE REWEIGHTING -------
733  if (lheDebug) {
734  std::cout << "Found " << lheReweighingIDs.size() << " reweighting weights" << std::endl;
735  }
736  std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
737 
738  std::stringstream pdfDoc;
739  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
740  bool found = false;
741  for (uint32_t lhaid : preferredPDFLHAIDs_) {
742  for (const auto& pw : pdfSetWeightIDs) {
743  if (pw.lhaIDs.first != lhaid && pw.lhaIDs.first != (lhaid + 1))
744  continue; // sometimes the first weight is not saved if that PDF is the nominal one for the sample
745  if (pw.wids.size() == 1)
746  continue; // only consider error sets
747  pdfDoc << pw.lhaIDs.first << " - " << pw.lhaIDs.second;
748  weightChoice->pdfWeightIDs = pw.wids;
749  if (maxPdfWeights_ < pw.wids.size()) {
750  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
751  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
752  }
753  weightChoice->pdfWeightsDoc = pdfDoc.str();
754  found = true;
755  break;
756  }
757  if (found)
758  break;
759  }
760  }
761  }
762  return weightChoice;
763  }
764 
765  // create an empty counter
766  std::unique_ptr<CounterMap> beginStream(edm::StreamID) const override { return std::make_unique<CounterMap>(); }
767  // inizialize to zero at begin run
768  void streamBeginRun(edm::StreamID id, edm::Run const&, edm::EventSetup const&) const override {
769  streamCache(id)->clear();
770  }
772  edm::LuminosityBlock const& lumiBlock,
773  edm::EventSetup const& eventSetup) const override {
774  auto counterMap = streamCache(id);
775  edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
776  lumiBlock.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
777  if (!genLumiInfoHead.isValid())
778  edm::LogWarning("LHETablesProducer")
779  << "No GenLumiInfoHeader product found, will not fill generator model string.\n";
780  counterMap->setLabel(genLumiInfoHead.isValid() ? genLumiInfoHead->configDescription() : "");
781  }
782  // create an empty counter
783  std::shared_ptr<CounterMap> globalBeginRunSummary(edm::Run const&, edm::EventSetup const&) const override {
784  return std::make_shared<CounterMap>();
785  }
786  // add this stream to the summary
788  edm::Run const&,
789  edm::EventSetup const&,
790  CounterMap* runCounterMap) const override {
791  runCounterMap->merge(*streamCache(id));
792  }
793  // nothing to do per se
794  void globalEndRunSummary(edm::Run const&, edm::EventSetup const&, CounterMap* runCounterMap) const override {}
795  // write the total to the run
796  void globalEndRunProduce(edm::Run& iRun, edm::EventSetup const&, CounterMap const* runCounterMap) const override {
797  auto out = std::make_unique<nanoaod::MergeableCounterTable>();
798 
799  for (auto x : runCounterMap->countermap) {
800  auto runCounter = &(x.second);
801  std::string label = std::string("_") + x.first;
802  std::string doclabel = (!x.first.empty()) ? (std::string(", for model label ") + x.first) : "";
803 
804  out->addInt("genEventCount" + label, "event count" + doclabel, runCounter->num);
805  out->addFloat("genEventSumw" + label, "sum of gen weights" + doclabel, runCounter->sumw);
806  out->addFloat("genEventSumw2" + label, "sum of gen (weight^2)" + doclabel, runCounter->sumw2);
807 
808  double norm = runCounter->sumw ? 1.0 / runCounter->sumw : 1;
809  auto sumScales = runCounter->sumScale;
810  for (auto& val : sumScales)
811  val *= norm;
812  out->addVFloat("LHEScaleSumw" + label,
813  "Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw" + doclabel,
814  sumScales);
815  auto sumPDFs = runCounter->sumPDF;
816  for (auto& val : sumPDFs)
817  val *= norm;
818  out->addVFloat(
819  "LHEPdfSumw" + label, "Sum of genEventWeight * LHEPdfWeight[i], divided by genEventSumw" + doclabel, sumPDFs);
820  if (!runCounter->sumRwgt.empty()) {
821  auto sumRwgts = runCounter->sumRwgt;
822  for (auto& val : sumRwgts)
823  val *= norm;
824  out->addVFloat("LHEReweightingSumw" + label,
825  "Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw" + doclabel,
826  sumRwgts);
827  }
828  if (!runCounter->sumNamed.empty()) { // it could be empty if there's no LHE info in the sample
829  for (unsigned int i = 0, n = namedWeightLabels_.size(); i < n; ++i) {
830  out->addFloat(
831  "LHESumw_" + namedWeightLabels_[i] + label,
832  "Sum of genEventWeight * LHEWeight_" + namedWeightLabels_[i] + ", divided by genEventSumw" + doclabel,
833  runCounter->sumNamed[i] * norm);
834  }
835  }
836  }
837  iRun.put(std::move(out));
838  }
839  // nothing to do here
840  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}
841 
844  desc.add<edm::InputTag>("genEvent", edm::InputTag("generator"))
845  ->setComment("tag for the GenEventInfoProduct, to get the main weight");
846  desc.add<edm::InputTag>("genLumiInfoHeader", edm::InputTag("generator"))
847  ->setComment("tag for the GenLumiInfoProduct, to get the model string");
848  desc.add<std::vector<edm::InputTag>>("lheInfo", std::vector<edm::InputTag>{{"externalLHEProducer"}, {"source"}})
849  ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
850 
852  prefpdf.add<std::string>("name");
853  prefpdf.add<uint32_t>("lhaid");
854  desc.addVPSet("preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())
855  ->setComment(
856  "LHA PDF Ids of the preferred PDF sets, in order of preference (the first matching one will be used)");
857  desc.add<std::vector<std::string>>("namedWeightIDs")->setComment("set of LHA weight IDs for named LHE weights");
858  desc.add<std::vector<std::string>>("namedWeightLabels")
859  ->setComment("output names for the namedWeightIDs (in the same order)");
860  desc.add<int32_t>("lheWeightPrecision")->setComment("Number of bits in the mantissa for LHE weights");
861  desc.add<uint32_t>("maxPdfWeights")->setComment("Maximum number of PDF weights to save (to crop NN replicas)");
862  desc.addOptionalUntracked<bool>("debug")->setComment("dump out all LHE information for one event");
863  descriptions.add("genWeightsTable", desc);
864  }
865 
866 protected:
868  const std::vector<edm::InputTag> lheLabel_;
869  const std::vector<edm::EDGetTokenT<LHEEventProduct>> lheTag_;
870  const std::vector<edm::EDGetTokenT<LHERunInfoProduct>> lheRunTag_;
872 
873  std::vector<uint32_t> preferredPDFLHAIDs_;
874  std::unordered_map<std::string, uint32_t> lhaNameToID_;
875  std::vector<std::string> namedWeightIDs_;
876  std::vector<std::string> namedWeightLabels_;
878  unsigned int maxPdfWeights_;
879 
880  mutable std::atomic<bool> debug_, debugRun_, hasIssuedWarning_;
881 };
882 
const std::vector< edm::EDGetTokenT< LHERunInfoProduct > > lheRunTag_
double originalXWGTUP() const
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:280
void setComment(std::string const &value)
void streamBeginRun(edm::StreamID id, edm::Run const &, edm::EventSetup const &) const override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
std::unique_ptr< CounterMap > beginStream(edm::StreamID) const override
void streamBeginLuminosityBlock(edm::StreamID id, edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &eventSetup) const override
const double w
Definition: UKUtility.cc:23
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const edm::EDGetTokenT< GenEventInfoProduct > genTag_
std::shared_ptr< DynamicWeightChoice > globalBeginRun(edm::Run const &iRun, edm::EventSetup const &) const override
Definition: weight.py:1
headers_const_iterator headers_end() const
Run const & getRun() const
Definition: Event.cc:108
double weight() const
std::atomic< bool > hasIssuedWarning_
std::unordered_map< std::string, uint32_t > lhaNameToID_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
uint16_t size_type
void fillLHEWeightTables(Counter *counter, const DynamicWeightChoice *weightChoice, double genWeight, const LHEEventProduct &lheProd, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outRwgt, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
void globalEndRunSummary(edm::Run const &, edm::EventSetup const &, CounterMap *runCounterMap) const override
const std::vector< edm::InputTag > lheLabel_
const std::vector< WGT > & weights() const
char const * label
std::function< unsigned int(align::ID)> Counter
std::shared_ptr< CounterMap > globalBeginRunSummary(edm::Run const &, edm::EventSetup const &) const override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
headers_const_iterator headers_begin() const
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:151
RunIndex index() const
Definition: Run.cc:21
double f[11][100]
#define end
Definition: vmac.h:39
void fillOnlyPSWeightTable(Counter *counter, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
const std::string & configDescription() const
std::vector< std::string > namedWeightLabels_
GenWeightsTableProducer(edm::ParameterSet const &params)
void put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Run.h:108
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:69
void streamEndRunSummary(edm::StreamID id, edm::Run const &, edm::EventSetup const &, CounterMap *runCounterMap) const override
void globalEndRunProduce(edm::Run &iRun, edm::EventSetup const &, CounterMap const *runCounterMap) const override
std::vector< uint32_t > preferredPDFLHAIDs_
HLT enums.
std::vector< double > & weights()
void produce(edm::StreamID id, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
std::vector< std::string > namedWeightIDs_
#define str(s)
const edm::EDGetTokenT< GenLumiInfoHeader > genLumiInfoHeadTag_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
def merge(dictlist, TELL=False)
Definition: MatrixUtil.py:194