CMS 3D CMS Logo

GenWeightsTableProducer.cc
Go to the documentation of this file.
15 #include "boost/algorithm/string.hpp"
16 
17 #include <memory>
18 
19 #include <vector>
20 #include <unordered_map>
21 #include <iostream>
22 #include <regex>
23 
24 namespace {
26  struct Counter {
27  Counter() : num(0), sumw(0), sumw2(0), sumPDF(), sumScale(), sumRwgt(), sumNamed(), sumPS() {}
28 
29  // the counters
30  long long num;
31  long double sumw;
32  long double sumw2;
33  std::vector<long double> sumPDF, sumScale, sumRwgt, sumNamed, sumPS;
34 
35  void clear() {
36  num = 0;
37  sumw = 0;
38  sumw2 = 0;
39  sumPDF.clear();
40  sumScale.clear();
41  sumRwgt.clear();
42  sumNamed.clear(), sumPS.clear();
43  }
44 
45  // inc the counters
46  void incGenOnly(double w) {
47  num++;
48  sumw += w;
49  sumw2 += (w * w);
50  }
51 
52  void incPSOnly(double w0, const std::vector<double>& wPS) {
53  if (!wPS.empty()) {
54  if (sumPS.empty())
55  sumPS.resize(wPS.size(), 0);
56  for (unsigned int i = 0, n = wPS.size(); i < n; ++i)
57  sumPS[i] += (w0 * wPS[i]);
58  }
59  }
60 
61  void incLHE(double w0,
62  const std::vector<double>& wScale,
63  const std::vector<double>& wPDF,
64  const std::vector<double>& wRwgt,
65  const std::vector<double>& wNamed,
66  const std::vector<double>& wPS) {
67  // add up weights
68  incGenOnly(w0);
69  // then add up variations
70  if (!wScale.empty()) {
71  if (sumScale.empty())
72  sumScale.resize(wScale.size(), 0);
73  for (unsigned int i = 0, n = wScale.size(); i < n; ++i)
74  sumScale[i] += (w0 * wScale[i]);
75  }
76  if (!wPDF.empty()) {
77  if (sumPDF.empty())
78  sumPDF.resize(wPDF.size(), 0);
79  for (unsigned int i = 0, n = wPDF.size(); i < n; ++i)
80  sumPDF[i] += (w0 * wPDF[i]);
81  }
82  if (!wRwgt.empty()) {
83  if (sumRwgt.empty())
84  sumRwgt.resize(wRwgt.size(), 0);
85  for (unsigned int i = 0, n = wRwgt.size(); i < n; ++i)
86  sumRwgt[i] += (w0 * wRwgt[i]);
87  }
88  if (!wNamed.empty()) {
89  if (sumNamed.empty())
90  sumNamed.resize(wNamed.size(), 0);
91  for (unsigned int i = 0, n = wNamed.size(); i < n; ++i)
92  sumNamed[i] += (w0 * wNamed[i]);
93  }
94  incPSOnly(w0, wPS);
95  }
96 
97  void merge(const Counter& other) {
98  num += other.num;
99  sumw += other.sumw;
100  sumw2 += other.sumw2;
101  if (sumScale.empty() && !other.sumScale.empty())
102  sumScale.resize(other.sumScale.size(), 0);
103  if (sumPDF.empty() && !other.sumPDF.empty())
104  sumPDF.resize(other.sumPDF.size(), 0);
105  if (sumRwgt.empty() && !other.sumRwgt.empty())
106  sumRwgt.resize(other.sumRwgt.size(), 0);
107  if (sumNamed.empty() && !other.sumNamed.empty())
108  sumNamed.resize(other.sumNamed.size(), 0);
109  if (sumPS.empty() && !other.sumPS.empty())
110  sumPS.resize(other.sumPS.size(), 0);
111  if (!other.sumScale.empty())
112  for (unsigned int i = 0, n = sumScale.size(); i < n; ++i)
113  sumScale[i] += other.sumScale[i];
114  if (!other.sumPDF.empty())
115  for (unsigned int i = 0, n = sumPDF.size(); i < n; ++i)
116  sumPDF[i] += other.sumPDF[i];
117  if (!other.sumRwgt.empty())
118  for (unsigned int i = 0, n = sumRwgt.size(); i < n; ++i)
119  sumRwgt[i] += other.sumRwgt[i];
120  if (!other.sumNamed.empty())
121  for (unsigned int i = 0, n = sumNamed.size(); i < n; ++i)
122  sumNamed[i] += other.sumNamed[i];
123  if (!other.sumPS.empty())
124  for (unsigned int i = 0, n = sumPS.size(); i < n; ++i)
125  sumPS[i] += other.sumPS[i];
126  }
127  };
128 
129  struct CounterMap {
130  std::map<std::string, Counter> countermap;
131  Counter* active_el = nullptr;
132  std::string active_label = "";
133  void merge(const CounterMap& other) {
134  for (const auto& y : other.countermap)
135  countermap[y.first].merge(y.second);
136  active_el = nullptr;
137  }
138  void clear() {
139  for (auto x : countermap)
140  x.second.clear();
141  active_el = nullptr;
142  active_label = "";
143  }
144  void setLabel(std::string label) {
145  active_el = &(countermap[label]);
146  active_label = label;
147  }
148  void checkLabelSet() {
149  if (!active_el)
150  throw cms::Exception("LogicError", "Called CounterMap::get() before setting the active label\n");
151  }
152  Counter* get() {
153  checkLabelSet();
154  return active_el;
155  }
156  std::string& getLabel() {
157  checkLabelSet();
158  return active_label;
159  }
160  };
161 
163  struct DynamicWeightChoice {
164  // choice of LHE weights
165  // ---- scale ----
166  std::vector<std::string> scaleWeightIDs;
167  std::string scaleWeightsDoc;
168  // ---- pdf ----
169  std::vector<std::string> pdfWeightIDs;
170  std::string pdfWeightsDoc;
171  // ---- rwgt ----
172  std::vector<std::string> rwgtIDs;
173  std::string rwgtWeightDoc;
174  };
175 
176  struct DynamicWeightChoiceGenInfo {
177  // choice of LHE weights
178  // ---- scale ----
179  std::vector<unsigned int> scaleWeightIDs;
180  std::string scaleWeightsDoc;
181  // ---- pdf ----
182  std::vector<unsigned int> pdfWeightIDs;
183  std::string pdfWeightsDoc;
184  // ---- ps ----
185  std::vector<unsigned int> defPSWeightIDs = {6, 7, 8, 9};
186  std::vector<unsigned int> defPSWeightIDs_alt = {27, 5, 26, 4};
187  bool matchPS_alt = false;
188  std::vector<unsigned int> psWeightIDs;
189  unsigned int psBaselineID = 1;
190  std::string psWeightsDoc;
191 
192  void setMissingWeight(int idx) { psWeightIDs[idx] = (matchPS_alt) ? defPSWeightIDs_alt[idx] : defPSWeightIDs[idx]; }
193 
194  bool empty() const { return scaleWeightIDs.empty() && pdfWeightIDs.empty() && psWeightIDs.empty(); }
195  };
196 
197  struct LumiCacheInfoHolder {
198  CounterMap countermap;
199  DynamicWeightChoiceGenInfo weightChoice;
200  void clear() {
201  countermap.clear();
202  weightChoice = DynamicWeightChoiceGenInfo();
203  }
204  };
205 
206  float stof_fortrancomp(const std::string& str) {
207  std::string::size_type match = str.find('d');
208  if (match != std::string::npos) {
209  std::string pre = str.substr(0, match);
210  std::string post = str.substr(match + 1);
211  return std::stof(pre) * std::pow(10.0f, std::stof(post));
212  } else {
213  return std::stof(str);
214  }
215  }
217  struct ScaleVarWeight {
218  std::string wid, label;
219  std::pair<float, float> scales;
220  ScaleVarWeight(const std::string& id, const std::string& text, const std::string& muR, const std::string& muF)
221  : wid(id), label(text), scales(stof_fortrancomp(muR), stof_fortrancomp(muF)) {}
222  bool operator<(const ScaleVarWeight& other) {
223  return (scales == other.scales ? wid < other.wid : scales < other.scales);
224  }
225  };
226  struct PDFSetWeights {
227  std::vector<std::string> wids;
228  std::pair<unsigned int, unsigned int> lhaIDs;
229  PDFSetWeights(const std::string& wid, unsigned int lhaID) : wids(1, wid), lhaIDs(lhaID, lhaID) {}
230  bool operator<(const PDFSetWeights& other) const { return lhaIDs < other.lhaIDs; }
231  void add(const std::string& wid, unsigned int lhaID) {
232  wids.push_back(wid);
233  lhaIDs.second = lhaID;
234  }
235  bool maybe_add(const std::string& wid, unsigned int lhaID) {
236  if (lhaID == lhaIDs.second + 1) {
237  lhaIDs.second++;
238  wids.push_back(wid);
239  return true;
240  } else {
241  return false;
242  }
243  }
244  };
245 } // namespace
246 
247 class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<LumiCacheInfoHolder>,
248  edm::RunCache<DynamicWeightChoice>,
249  edm::RunSummaryCache<CounterMap>,
250  edm::EndRunProducer> {
251 public:
253  : genTag_(consumes<GenEventInfoProduct>(params.getParameter<edm::InputTag>("genEvent"))),
254  lheLabel_(params.getParameter<std::vector<edm::InputTag>>("lheInfo")),
255  lheTag_(edm::vector_transform(lheLabel_,
256  [this](const edm::InputTag& tag) { return mayConsume<LHEEventProduct>(tag); })),
257  lheRunTag_(edm::vector_transform(
258  lheLabel_, [this](const edm::InputTag& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
259  genLumiInfoHeadTag_(
260  mayConsume<GenLumiInfoHeader, edm::InLumi>(params.getParameter<edm::InputTag>("genLumiInfoHeader"))),
261  namedWeightIDs_(params.getParameter<std::vector<std::string>>("namedWeightIDs")),
262  namedWeightLabels_(params.getParameter<std::vector<std::string>>("namedWeightLabels")),
263  lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")),
264  maxPdfWeights_(params.getParameter<uint32_t>("maxPdfWeights")),
265  keepAllPSWeights_(params.getParameter<bool>("keepAllPSWeights")),
266  debug_(params.getUntrackedParameter<bool>("debug", false)),
267  debugRun_(debug_.load()),
268  hasIssuedWarning_(false),
269  psWeightWarning_(false) {
270  produces<nanoaod::FlatTable>();
271  produces<std::string>("genModel");
272  produces<nanoaod::FlatTable>("LHEScale");
273  produces<nanoaod::FlatTable>("LHEPdf");
274  produces<nanoaod::FlatTable>("LHEReweighting");
275  produces<nanoaod::FlatTable>("LHENamed");
276  produces<nanoaod::FlatTable>("PS");
277  produces<nanoaod::MergeableCounterTable, edm::Transition::EndRun>();
278  if (namedWeightIDs_.size() != namedWeightLabels_.size()) {
279  throw cms::Exception("Configuration", "Size mismatch between namedWeightIDs & namedWeightLabels");
280  }
281  for (const edm::ParameterSet& pdfps : params.getParameter<std::vector<edm::ParameterSet>>("preferredPDFs")) {
282  const std::string& name = pdfps.getParameter<std::string>("name");
283  uint32_t lhaid = pdfps.getParameter<uint32_t>("lhaid");
284  preferredPDFLHAIDs_.push_back(lhaid);
285  lhaNameToID_[name] = lhaid;
286  lhaNameToID_[name + ".LHgrid"] = lhaid;
287  }
288  }
289 
291 
292  void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
293  // get my counter for weights
294  Counter* counter = streamCache(id)->countermap.get();
295 
296  // generator information (always available)
298  iEvent.getByToken(genTag_, genInfo);
299  double weight = genInfo->weight();
300 
301  // table for gen info, always available
302  auto out = std::make_unique<nanoaod::FlatTable>(1, "genWeight", true);
303  out->setDoc("generator weight");
304  out->addColumnValue<float>("", weight, "generator weight", nanoaod::FlatTable::FloatColumn);
305  iEvent.put(std::move(out));
306 
307  std::string model_label = streamCache(id)->countermap.getLabel();
308  auto outM = std::make_unique<std::string>((!model_label.empty()) ? std::string("GenModel_") + model_label : "");
309  iEvent.put(std::move(outM), "genModel");
310  bool getLHEweightsFromGenInfo = !model_label.empty();
311 
312  // tables for LHE weights, may not be filled
313  std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
314  std::unique_ptr<nanoaod::FlatTable> genPSTab;
315 
317  for (const auto& lheTag : lheTag_) {
318  iEvent.getByToken(lheTag, lheInfo);
319  if (lheInfo.isValid()) {
320  break;
321  }
322  }
323 
324  const auto genWeightChoice = &(streamCache(id)->weightChoice);
325  if (lheInfo.isValid()) {
326  if (getLHEweightsFromGenInfo && !hasIssuedWarning_.exchange(true))
327  edm::LogWarning("LHETablesProducer")
328  << "Found both a LHEEventProduct and a GenLumiInfoHeader: will only save weights from LHEEventProduct.\n";
329  // get the dynamic choice of weights
330  const DynamicWeightChoice* weightChoice = runCache(iEvent.getRun().index());
331  // go fill tables
332  fillLHEWeightTables(counter,
333  weightChoice,
334  genWeightChoice,
335  weight,
336  *lheInfo,
337  *genInfo,
338  lheScaleTab,
339  lhePdfTab,
340  lheRwgtTab,
341  lheNamedTab,
342  genPSTab);
343  } else if (getLHEweightsFromGenInfo) {
344  fillLHEPdfWeightTablesFromGenInfo(
345  counter, genWeightChoice, weight, *genInfo, lheScaleTab, lhePdfTab, lheNamedTab, genPSTab);
346  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
347  //lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
348  //genPSTab = std::make_unique<nanoaod::FlatTable>(1, "PSWeight", true);
349  } else {
350  // Still try to add the PS weights
351  fillOnlyPSWeightTable(counter, genWeightChoice, weight, *genInfo, genPSTab);
352  // make dummy values
353  lheScaleTab = std::make_unique<nanoaod::FlatTable>(1, "LHEScaleWeights", true);
354  lhePdfTab = std::make_unique<nanoaod::FlatTable>(1, "LHEPdfWeights", true);
355  lheRwgtTab = std::make_unique<nanoaod::FlatTable>(1, "LHEReweightingWeights", true);
356  lheNamedTab = std::make_unique<nanoaod::FlatTable>(1, "LHENamedWeights", true);
357  if (!hasIssuedWarning_.exchange(true)) {
358  edm::LogWarning("LHETablesProducer") << "No LHEEventProduct, so there will be no LHE Tables\n";
359  }
360  }
361 
362  iEvent.put(std::move(lheScaleTab), "LHEScale");
363  iEvent.put(std::move(lhePdfTab), "LHEPdf");
364  iEvent.put(std::move(lheRwgtTab), "LHEReweighting");
365  iEvent.put(std::move(lheNamedTab), "LHENamed");
366  iEvent.put(std::move(genPSTab), "PS");
367  }
368 
370  const DynamicWeightChoice* weightChoice,
371  const DynamicWeightChoiceGenInfo* genWeightChoice,
372  double genWeight,
373  const LHEEventProduct& lheProd,
374  const GenEventInfoProduct& genProd,
375  std::unique_ptr<nanoaod::FlatTable>& outScale,
376  std::unique_ptr<nanoaod::FlatTable>& outPdf,
377  std::unique_ptr<nanoaod::FlatTable>& outRwgt,
378  std::unique_ptr<nanoaod::FlatTable>& outNamed,
379  std::unique_ptr<nanoaod::FlatTable>& outPS) const {
380  bool lheDebug = debug_.exchange(
381  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
382 
383  const std::vector<std::string>& scaleWeightIDs = weightChoice->scaleWeightIDs;
384  const std::vector<std::string>& pdfWeightIDs = weightChoice->pdfWeightIDs;
385  const std::vector<std::string>& rwgtWeightIDs = weightChoice->rwgtIDs;
386 
387  double w0 = lheProd.originalXWGTUP();
388 
389  std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1),
390  wNamed(namedWeightIDs_.size(), 1);
391  for (auto& weight : lheProd.weights()) {
392  if (lheDebug)
393  printf("Weight %+9.5f rel %+9.5f for id %s\n", weight.wgt, weight.wgt / w0, weight.id.c_str());
394  // now we do it slowly, can be optimized
395  auto mScale = std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(), weight.id);
396  if (mScale != scaleWeightIDs.end())
397  wScale[mScale - scaleWeightIDs.begin()] = weight.wgt / w0;
398 
399  auto mPDF = std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(), weight.id);
400  if (mPDF != pdfWeightIDs.end())
401  wPDF[mPDF - pdfWeightIDs.begin()] = weight.wgt / w0;
402 
403  auto mRwgt = std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(), weight.id);
404  if (mRwgt != rwgtWeightIDs.end())
405  wRwgt[mRwgt - rwgtWeightIDs.begin()] = weight.wgt / w0;
406 
407  auto mNamed = std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(), weight.id);
408  if (mNamed != namedWeightIDs_.end())
409  wNamed[mNamed - namedWeightIDs_.begin()] = weight.wgt / w0;
410  }
411 
412  std::vector<double> wPS;
413  std::string psWeightDocStr;
414  setPSWeightInfo(genProd.weights(), genWeightChoice, wPS, psWeightDocStr);
415 
416  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
417  outPS->addColumn<float>("", wPS, psWeightDocStr, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
418 
419  outScale = std::make_unique<nanoaod::FlatTable>(wScale.size(), "LHEScaleWeight", false);
420  outScale->addColumn<float>(
421  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
422 
423  outPdf = std::make_unique<nanoaod::FlatTable>(wPDF.size(), "LHEPdfWeight", false);
424  outPdf->addColumn<float>(
425  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
426 
427  outRwgt = std::make_unique<nanoaod::FlatTable>(wRwgt.size(), "LHEReweightingWeight", false);
428  outRwgt->addColumn<float>(
429  "", wRwgt, weightChoice->rwgtWeightDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
430 
431  outNamed = std::make_unique<nanoaod::FlatTable>(1, "LHEWeight", true);
432  outNamed->addColumnValue<float>("originalXWGTUP",
433  lheProd.originalXWGTUP(),
434  "Nominal event weight in the LHE file",
436  for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
437  outNamed->addColumnValue<float>(namedWeightLabels_[i],
438  wNamed[i],
439  "LHE weight for id " + namedWeightIDs_[i] + ", relative to nominal",
441  lheWeightPrecision_);
442  }
443 
444  counter->incLHE(genWeight, wScale, wPDF, wRwgt, wNamed, wPS);
445  }
446 
448  const DynamicWeightChoiceGenInfo* weightChoice,
449  double genWeight,
450  const GenEventInfoProduct& genProd,
451  std::unique_ptr<nanoaod::FlatTable>& outScale,
452  std::unique_ptr<nanoaod::FlatTable>& outPdf,
453  std::unique_ptr<nanoaod::FlatTable>& outNamed,
454  std::unique_ptr<nanoaod::FlatTable>& outPS) const {
455  const std::vector<unsigned int>& scaleWeightIDs = weightChoice->scaleWeightIDs;
456  const std::vector<unsigned int>& pdfWeightIDs = weightChoice->pdfWeightIDs;
457 
458  auto weights = genProd.weights();
459  double w0 = (weights.size() > 1) ? weights.at(1) : 1.;
460  double originalXWGTUP = (weights.size() > 1) ? weights.at(1) : 1.;
461 
462  std::vector<double> wScale, wPDF, wPS;
463  for (auto id : scaleWeightIDs)
464  wScale.push_back(weights.at(id) / w0);
465  for (auto id : pdfWeightIDs) {
466  wPDF.push_back(weights.at(id) / w0);
467  }
468 
469  std::string psWeightsDocStr;
470  setPSWeightInfo(genProd.weights(), weightChoice, wPS, psWeightsDocStr);
471 
472  outScale = std::make_unique<nanoaod::FlatTable>(wScale.size(), "LHEScaleWeight", false);
473  outScale->addColumn<float>(
474  "", wScale, weightChoice->scaleWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
475 
476  outPdf = std::make_unique<nanoaod::FlatTable>(wPDF.size(), "LHEPdfWeight", false);
477  outPdf->addColumn<float>(
478  "", wPDF, weightChoice->pdfWeightsDoc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
479 
480  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
481  outPS->addColumn<float>("", wPS, psWeightsDocStr, nanoaod::FlatTable::FloatColumn,
482  lheWeightPrecision_);
483 
484  outNamed = std::make_unique<nanoaod::FlatTable>(1, "LHEWeight", true);
485  outNamed->addColumnValue<float>(
486  "originalXWGTUP", originalXWGTUP, "Nominal event weight in the LHE file", nanoaod::FlatTable::FloatColumn);
487  /*for (unsigned int i = 0, n = wNamed.size(); i < n; ++i) {
488  outNamed->addColumnValue<float>(namedWeightLabels_[i], wNamed[i], "LHE weight for id "+namedWeightIDs_[i]+", relative to nominal", nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
489  }*/
490 
491  counter->incLHE(genWeight, wScale, wPDF, std::vector<double>(), std::vector<double>(), wPS);
492  }
493 
495  const DynamicWeightChoiceGenInfo* genWeightChoice,
496  double genWeight,
497  const GenEventInfoProduct& genProd,
498  std::unique_ptr<nanoaod::FlatTable>& outPS) const {
499  std::vector<double> wPS;
500  std::string psWeightDocStr;
501  setPSWeightInfo(genProd.weights(), genWeightChoice, wPS, psWeightDocStr);
502  outPS = std::make_unique<nanoaod::FlatTable>(wPS.size(), "PSWeight", false);
503  outPS->addColumn<float>("", wPS, psWeightDocStr, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
504 
505  counter->incGenOnly(genWeight);
506  counter->incPSOnly(genWeight, wPS);
507  }
508 
509  void setPSWeightInfo(const std::vector<double>& genWeights,
510  const DynamicWeightChoiceGenInfo* genWeightChoice,
511  std::vector<double>& wPS,
512  std::string& psWeightDocStr) const {
513  wPS.clear();
514  // isRegularPSSet = keeping all weights and the weights are a usual size, ie
515  // all weights are PS weights (don't use header incase missing names)
516  bool isRegularPSSet = keepAllPSWeights_ && (genWeights.size() == 14 || genWeights.size() == 46);
517  if (!genWeightChoice->psWeightIDs.empty() && !isRegularPSSet) {
518  psWeightDocStr = genWeightChoice->psWeightsDoc;
519  double psNom = genWeights.at(genWeightChoice->psBaselineID);
520  for (auto wgtidx : genWeightChoice->psWeightIDs) {
521  wPS.push_back(genWeights.at(wgtidx) / psNom);
522  }
523  } else {
524  int vectorSize =
525  keepAllPSWeights_ ? (genWeights.size() - 2) : ((genWeights.size() == 14 || genWeights.size() == 46) ? 4 : 1);
526 
527  if (vectorSize > 1) {
528  double nominal = genWeights.at(1); // Called 'Baseline' in GenLumiInfoHeader
529  if (keepAllPSWeights_) {
530  for (int i = 0; i < vectorSize; i++) {
531  wPS.push_back(genWeights.at(i + 2) / nominal);
532  }
533  psWeightDocStr = "All PS weights (w_var / w_nominal)";
534  } else {
535  if (!psWeightWarning_.exchange(true))
536  edm::LogWarning("LHETablesProducer")
537  << "GenLumiInfoHeader not found: Central PartonShower weights will fill with the 6-10th entries \n"
538  << " This may incorrect for some mcs (madgraph 2.6.1 with its `isr:murfact=0.5` have a differnt "
539  "order )";
540  for (std::size_t i = 6; i < 10; i++) {
541  wPS.push_back(genWeights.at(i) / nominal);
542  }
543  psWeightDocStr =
544  "PS weights (w_var / w_nominal); [0] is ISR=2 FSR=1; [1] is ISR=1 FSR=2"
545  "[2] is ISR=0.5 FSR=1; [3] is ISR=1 FSR=0.5;";
546  }
547  } else {
548  wPS.push_back(1.0);
549  psWeightDocStr = "dummy PS weight (1.0) ";
550  }
551  }
552  }
553 
554  // create an empty counter
555  std::shared_ptr<DynamicWeightChoice> globalBeginRun(edm::Run const& iRun, edm::EventSetup const&) const override {
557 
558  bool lheDebug = debugRun_.exchange(
559  false); // make sure only the first thread dumps out this (even if may still be mixed up with other output, but nevermind)
560  auto weightChoice = std::make_shared<DynamicWeightChoice>();
561 
562  // getByToken throws since we're not in the endRun (see https://github.com/cms-sw/cmssw/pull/18499)
563  //if (iRun.getByToken(lheRunTag_, lheInfo)) {
564  for (const auto& lheLabel : lheLabel_) {
565  iRun.getByLabel(lheLabel, lheInfo);
566  if (lheInfo.isValid()) {
567  break;
568  }
569  }
570  if (lheInfo.isValid()) {
571  std::vector<ScaleVarWeight> scaleVariationIDs;
572  std::vector<PDFSetWeights> pdfSetWeightIDs;
573  std::vector<std::string> lheReweighingIDs;
574  bool isFirstGroup = true;
575 
576  std::regex weightgroupmg26x("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
577  std::regex weightgroup("<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
578  std::regex weightgroupRwgt("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
579  std::regex endweightgroup("</weightgroup>");
580  std::regex scalewmg26x(
581  "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"("
582  "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
583  std::regex scalewmg26xNew(
584  "<weight\\s*((?:[mM][uU][fF]|facscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Rr]|renscfact)=\"(\\S+)\").+id=\"(\\d+)\"(."
585  "*)?</weight>");
586 
587  //<weight MUF="1.0" MUR="2.0" PDF="306000" id="1006"> MUR=2.0 </weight>
588  std::regex scalew(
589  "<weight\\s+(?:.*\\s+)?id=\"(\\d+|\\d+-NNLOPS)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)"
590  "=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
591  std::regex pdfw(
592  "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
593  std::regex pdfwOld("<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
594  std::regex pdfwmg26x(
595  "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF "
596  "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
597  "weight>");
598  //<weightgroup combine="symmhessian+as" name="NNPDF31_nnlo_as_0118_mc_hessian_pdfas">
599 
600  //<weight MUF="1.0" MUR="1.0" PDF="325300" id="1048"> PDF=325300 MemberID=0 </weight>
601  std::regex pdfwmg26xNew(
602  "<weight\\s+MUF=\"(?:\\S+)\"\\s*MUR=\"(?:\\S+)\"\\s*PDF=\"(?:\\S+)\"\\s*id=\"(\\S+)\"\\s*>"
603  "\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
604  "weight>");
605 
606  std::regex rwgt("<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
607  std::smatch groups;
608  for (auto iter = lheInfo->headers_begin(), end = lheInfo->headers_end(); iter != end; ++iter) {
609  if (iter->tag() != "initrwgt") {
610  if (lheDebug)
611  std::cout << "Skipping LHE header with tag" << iter->tag() << std::endl;
612  continue;
613  }
614  if (lheDebug)
615  std::cout << "Found LHE header with tag" << iter->tag() << std::endl;
616  std::vector<std::string> lines = iter->lines();
617  bool missed_weightgroup =
618  false; //Needed because in some of the samples ( produced with MG26X ) a small part of the header info is ordered incorrectly
619  bool ismg26x = false;
620  bool ismg26xNew = false;
621  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
622  ++iLine) { //First start looping through the lines to see which weightgroup pattern is matched
623  boost::replace_all(lines[iLine], "&lt;", "<");
624  boost::replace_all(lines[iLine], "&gt;", ">");
625  if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
626  ismg26x = true;
627  } else if (std::regex_search(lines[iLine], groups, scalewmg26xNew) ||
628  std::regex_search(lines[iLine], groups, pdfwmg26xNew)) {
629  ismg26xNew = true;
630  }
631  }
632  for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
633  if (lheDebug)
634  std::cout << lines[iLine];
635  if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
636  std::string groupname = groups.str(2);
637  if (ismg26x)
638  groupname = groups.str(1);
639  if (lheDebug)
640  std::cout << ">>> Looks like the beginning of a weight group for '" << groupname << "'" << std::endl;
641  if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation" || isFirstGroup) {
642  if (lheDebug && groupname.find("scale_variation") != 0 && groupname != "Central scale variation")
643  std::cout << ">>> First weight is not scale variation, but assuming is the Central Weight" << std::endl;
644  else if (lheDebug)
645  std::cout << ">>> Looks like scale variation for theory uncertainties" << std::endl;
646  isFirstGroup = false;
647  for (++iLine; iLine < nLines; ++iLine) {
648  if (lheDebug) {
649  std::cout << " " << lines[iLine];
650  }
651  if (std::regex_search(
652  lines[iLine], groups, ismg26x ? scalewmg26x : (ismg26xNew ? scalewmg26xNew : scalew))) {
653  if (lheDebug)
654  std::cout << " >>> Scale weight " << groups[1].str() << " for " << groups[3].str() << " , "
655  << groups[4].str() << " , " << groups[5].str() << std::endl;
656  if (ismg26xNew) {
657  scaleVariationIDs.emplace_back(groups.str(4), groups.str(1), groups.str(3), groups.str(2));
658  } else {
659  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
660  }
661  } else if (std::regex_search(lines[iLine], endweightgroup)) {
662  if (lheDebug)
663  std::cout << ">>> Looks like the end of a weight group" << std::endl;
664  if (!missed_weightgroup) {
665  break;
666  } else
667  missed_weightgroup = false;
668  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
669  if (lheDebug)
670  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
671  "of the group."
672  << std::endl;
673  if (ismg26x || ismg26xNew)
674  missed_weightgroup = true;
675  --iLine; // rewind by one, and go back to the outer loop
676  break;
677  }
678  }
679  } else if (groupname == "PDF_variation" || groupname.find("PDF_variation ") == 0) {
680  if (lheDebug)
681  std::cout << ">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
682  for (++iLine; iLine < nLines; ++iLine) {
683  if (lheDebug)
684  std::cout << " " << lines[iLine];
685  if (std::regex_search(lines[iLine], groups, pdfw)) {
686  unsigned int lhaID = std::stoi(groups.str(2));
687  if (lheDebug)
688  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
689  << std::endl;
690  if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
691  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
692  }
693  } else if (std::regex_search(lines[iLine], endweightgroup)) {
694  if (lheDebug)
695  std::cout << ">>> Looks like the end of a weight group" << std::endl;
696  if (!missed_weightgroup) {
697  break;
698  } else
699  missed_weightgroup = false;
700  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
701  if (lheDebug)
702  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
703  "of the group."
704  << std::endl;
705  if (ismg26x || ismg26xNew)
706  missed_weightgroup = true;
707  --iLine; // rewind by one, and go back to the outer loop
708  break;
709  }
710  }
711  } else if (groupname == "PDF_variation1" || groupname == "PDF_variation2") {
712  if (lheDebug)
713  std::cout << ">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
714  unsigned int lastid = 0;
715  for (++iLine; iLine < nLines; ++iLine) {
716  if (lheDebug)
717  std::cout << " " << lines[iLine];
718  if (std::regex_search(lines[iLine], groups, pdfw)) {
719  unsigned int id = std::stoi(groups.str(1));
720  unsigned int lhaID = std::stoi(groups.str(2));
721  if (lheDebug)
722  std::cout << " >>> PDF weight " << groups.str(1) << " for " << groups.str(2) << " = " << lhaID
723  << std::endl;
724  if (id != (lastid + 1) || pdfSetWeightIDs.empty()) {
725  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
726  } else {
727  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
728  }
729  lastid = id;
730  } else if (std::regex_search(lines[iLine], endweightgroup)) {
731  if (lheDebug)
732  std::cout << ">>> Looks like the end of a weight group" << std::endl;
733  if (!missed_weightgroup) {
734  break;
735  } else
736  missed_weightgroup = false;
737  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
738  if (lheDebug)
739  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
740  "of the group."
741  << std::endl;
742  if (ismg26x || ismg26xNew)
743  missed_weightgroup = true;
744  --iLine; // rewind by one, and go back to the outer loop
745  break;
746  }
747  }
748  } else if (lhaNameToID_.find(groupname) != lhaNameToID_.end()) {
749  if (lheDebug)
750  std::cout << ">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
751  unsigned int firstLhaID = lhaNameToID_.find(groupname)->second;
752  bool first = true;
753  for (++iLine; iLine < nLines; ++iLine) {
754  if (lheDebug)
755  std::cout << " " << lines[iLine];
756  if (std::regex_search(
757  lines[iLine], groups, ismg26x ? pdfwmg26x : (ismg26xNew ? pdfwmg26xNew : pdfwOld))) {
758  unsigned int member = 0;
759  if (!ismg26x && !ismg26xNew) {
760  member = std::stoi(groups.str(2));
761  } else if (ismg26xNew) {
762  if (!groups.str(3).empty()) {
763  member = std::stoi(groups.str(3));
764  }
765  } else {
766  if (!groups.str(4).empty()) {
767  member = std::stoi(groups.str(4));
768  }
769  }
770  unsigned int lhaID = member + firstLhaID;
771  if (lheDebug)
772  std::cout << " >>> PDF weight " << groups.str(1) << " for " << member << " = " << lhaID
773  << std::endl;
774  //if (member == 0) continue; // let's keep also the central value for now
775  if (first) {
776  pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
777  first = false;
778  } else {
779  pdfSetWeightIDs.back().add(groups.str(1), lhaID);
780  }
781  } else if (std::regex_search(lines[iLine], endweightgroup)) {
782  if (lheDebug)
783  std::cout << ">>> Looks like the end of a weight group" << std::endl;
784  if (!missed_weightgroup) {
785  break;
786  } else
787  missed_weightgroup = false;
788  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
789  if (lheDebug)
790  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
791  "of the group."
792  << std::endl;
793  if (ismg26x || ismg26xNew)
794  missed_weightgroup = true;
795  --iLine; // rewind by one, and go back to the outer loop
796  break;
797  }
798  }
799  } else if (groupname == "mass_variation" || groupname == "sthw2_variation" ||
800  groupname == "width_variation") {
801  if (lheDebug)
802  std::cout << ">>> Looks like an EW parameter weight" << std::endl;
803  for (++iLine; iLine < nLines; ++iLine) {
804  if (lheDebug)
805  std::cout << " " << lines[iLine];
806  if (std::regex_search(lines[iLine], groups, rwgt)) {
807  std::string rwgtID = groups.str(1);
808  if (lheDebug)
809  std::cout << " >>> LHE reweighting weight: " << rwgtID << std::endl;
810  if (std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
811  // we're only interested in the beggining of the block
812  lheReweighingIDs.emplace_back(rwgtID);
813  }
814  } else if (std::regex_search(lines[iLine], endweightgroup)) {
815  if (lheDebug)
816  std::cout << ">>> Looks like the end of a weight group" << std::endl;
817  }
818  }
819  } else {
820  for (++iLine; iLine < nLines; ++iLine) {
821  if (lheDebug)
822  std::cout << " " << lines[iLine];
823  if (std::regex_search(lines[iLine], groups, endweightgroup)) {
824  if (lheDebug)
825  std::cout << ">>> Looks like the end of a weight group" << std::endl;
826  if (!missed_weightgroup) {
827  break;
828  } else
829  missed_weightgroup = false;
830  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
831  if (lheDebug)
832  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
833  "of the group."
834  << std::endl;
835  if (ismg26x || ismg26xNew)
836  missed_weightgroup = true;
837  --iLine; // rewind by one, and go back to the outer loop
838  break;
839  }
840  }
841  }
842  } else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
843  std::string groupname = groups.str(1);
844  if (groupname.find("mg_reweighting") != std::string::npos) {
845  if (lheDebug)
846  std::cout << ">>> Looks like a LHE weights for reweighting" << std::endl;
847  for (++iLine; iLine < nLines; ++iLine) {
848  if (lheDebug)
849  std::cout << " " << lines[iLine];
850  if (std::regex_search(lines[iLine], groups, rwgt)) {
851  std::string rwgtID = groups.str(1);
852  if (lheDebug)
853  std::cout << " >>> LHE reweighting weight: " << rwgtID << std::endl;
854  if (std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
855  // we're only interested in the beggining of the block
856  lheReweighingIDs.emplace_back(rwgtID);
857  }
858  } else if (std::regex_search(lines[iLine], endweightgroup)) {
859  if (lheDebug)
860  std::cout << ">>> Looks like the end of a weight group" << std::endl;
861  if (!missed_weightgroup) {
862  break;
863  } else
864  missed_weightgroup = false;
865  } else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
866  if (lheDebug)
867  std::cout << ">>> Looks like the beginning of a new weight group, I will assume I missed the end "
868  "of the group."
869  << std::endl;
870  if (ismg26x)
871  missed_weightgroup = true;
872  --iLine; // rewind by one, and go back to the outer loop
873  break;
874  }
875  }
876  }
877  }
878  }
879  //std::cout << "============= END [ " << iter->tag() << " ] ============ \n\n" << std::endl;
880 
881  // ----- SCALE VARIATIONS -----
882  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
883  if (lheDebug)
884  std::cout << "Found " << scaleVariationIDs.size() << " scale variations: " << std::endl;
885  std::stringstream scaleDoc;
886  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
887  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
888  const auto& sw = scaleVariationIDs[isw];
889  if (isw)
890  scaleDoc << "; ";
891  scaleDoc << "[" << isw << "] is " << sw.label;
892  weightChoice->scaleWeightIDs.push_back(sw.wid);
893  if (lheDebug)
894  printf(" id %s: scales ren = % .2f fact = % .2f text = %s\n",
895  sw.wid.c_str(),
896  sw.scales.first,
897  sw.scales.second,
898  sw.label.c_str());
899  }
900  if (!scaleVariationIDs.empty())
901  weightChoice->scaleWeightsDoc = scaleDoc.str();
902 
903  // ------ PDF VARIATIONS (take the preferred one) -----
904  if (lheDebug) {
905  std::cout << "Found " << pdfSetWeightIDs.size() << " PDF set errors: " << std::endl;
906  for (const auto& pw : pdfSetWeightIDs)
907  printf("lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
908  pw.lhaIDs.first,
909  pw.lhaIDs.second,
910  pw.wids.size(),
911  pw.wids.front().c_str());
912  }
913 
914  // ------ LHE REWEIGHTING -------
915  if (lheDebug) {
916  std::cout << "Found " << lheReweighingIDs.size() << " reweighting weights" << std::endl;
917  }
918  std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
919 
920  std::stringstream pdfDoc;
921  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
922  bool found = false;
923  for (const auto& pw : pdfSetWeightIDs) {
924  for (uint32_t lhaid : preferredPDFLHAIDs_) {
925  if (pw.lhaIDs.first != lhaid && pw.lhaIDs.first != (lhaid + 1))
926  continue; // sometimes the first weight is not saved if that PDF is the nominal one for the sample
927  if (pw.wids.size() == 1)
928  continue; // only consider error sets
929  pdfDoc << pw.lhaIDs.first << " - " << pw.lhaIDs.second;
930  weightChoice->pdfWeightIDs = pw.wids;
931  if (maxPdfWeights_ < pw.wids.size()) {
932  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
933  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
934  }
935  weightChoice->pdfWeightsDoc = pdfDoc.str();
936  found = true;
937  break;
938  }
939  if (found)
940  break;
941  }
942  }
943  }
944  return weightChoice;
945  }
946 
947  // create an empty counter
948  std::unique_ptr<LumiCacheInfoHolder> beginStream(edm::StreamID) const override {
949  return std::make_unique<LumiCacheInfoHolder>();
950  }
951  // inizialize to zero at begin run
952  void streamBeginRun(edm::StreamID id, edm::Run const&, edm::EventSetup const&) const override {
953  streamCache(id)->clear();
954  }
956  edm::LuminosityBlock const& lumiBlock,
957  edm::EventSetup const& eventSetup) const override {
958  auto counterMap = &(streamCache(id)->countermap);
959  edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
960  lumiBlock.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
961  if (!genLumiInfoHead.isValid())
962  edm::LogWarning("LHETablesProducer")
963  << "No GenLumiInfoHeader product found, will not fill generator model string.\n";
964 
966  if (genLumiInfoHead.isValid()) {
967  label = genLumiInfoHead->configDescription();
968  boost::replace_all(label, "-", "_");
969  boost::replace_all(label, "/", "_");
970  }
971  counterMap->setLabel(label);
972 
973  if (genLumiInfoHead.isValid()) {
974  auto weightChoice = &(streamCache(id)->weightChoice);
975 
976  std::vector<ScaleVarWeight> scaleVariationIDs;
977  std::vector<PDFSetWeights> pdfSetWeightIDs;
978  weightChoice->psWeightIDs.clear();
979 
980  std::regex scalew("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+)\\,\\s+mur=(\\S+)\\smuf=(\\S+)");
981  std::regex pdfw("LHE,\\s+id\\s+=\\s+(\\d+),\\s+(.+),\\s+Member\\s+(\\d+)\\s+of\\ssets\\s+(\\w+\\b)");
982  std::regex mainPSw("sr(Def|:murfac=)(Hi|Lo|_dn|_up|0.5|2.0)");
983  std::smatch groups;
984  auto weightNames = genLumiInfoHead->weightNames();
985  std::unordered_map<std::string, uint32_t> knownPDFSetsFromGenInfo_;
986  unsigned int weightIter = 0;
987  for (const auto& line : weightNames) {
988  if (std::regex_search(line, groups, scalew)) { // scale variation
989  auto id = groups.str(1);
990  auto group = groups.str(2);
991  auto mur = groups.str(3);
992  auto muf = groups.str(4);
993  if (group.find("Central scale variation") != std::string::npos)
994  scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
995  } else if (std::regex_search(line, groups, pdfw)) { // PDF variation
996  auto id = groups.str(1);
997  auto group = groups.str(2);
998  auto memberid = groups.str(3);
999  auto pdfset = groups.str(4);
1000  if (group.find(pdfset) != std::string::npos) {
1001  if (knownPDFSetsFromGenInfo_.find(pdfset) == knownPDFSetsFromGenInfo_.end()) {
1002  knownPDFSetsFromGenInfo_[pdfset] = std::atoi(id.c_str());
1003  pdfSetWeightIDs.emplace_back(id, std::atoi(id.c_str()));
1004  } else
1005  pdfSetWeightIDs.back().add(id, std::atoi(id.c_str()));
1006  }
1007  } else if (line == "Baseline") {
1008  weightChoice->psBaselineID = weightIter;
1009  } else if (line.find("isr") != std::string::npos || line.find("fsr") != std::string::npos) {
1010  weightChoice->matchPS_alt = line.find("sr:") != std::string::npos; // (f/i)sr: for new weights
1011  if (keepAllPSWeights_) {
1012  weightChoice->psWeightIDs.push_back(weightIter); // PS variations
1013  } else if (std::regex_search(line, groups, mainPSw)) {
1014  if (weightChoice->psWeightIDs.empty())
1015  weightChoice->psWeightIDs = std::vector<unsigned int>(4, -1);
1016  int psIdx = (line.find("fsr") != std::string::npos) ? 1 : 0;
1017  psIdx += (groups.str(2) == "Hi" || groups.str(2) == "_up" || groups.str(2) == "2.0") ? 0 : 2;
1018  weightChoice->psWeightIDs[psIdx] = weightIter;
1019  }
1020  }
1021  weightIter++;
1022  }
1023  if (keepAllPSWeights_) {
1024  weightChoice->psWeightsDoc = "All PS weights (w_var / w_nominal)";
1025  } else if (weightChoice->psWeightIDs.size() == 4) {
1026  weightChoice->psWeightsDoc =
1027  "PS weights (w_var / w_nominal); [0] is ISR=2 FSR=1; [1] is ISR=1 FSR=2"
1028  "[2] is ISR=0.5 FSR=1; [3] is ISR=1 FSR=0.5;";
1029  for (int i = 0; i < 4; i++) {
1030  if (static_cast<int>(weightChoice->psWeightIDs[i]) == -1)
1031  weightChoice->setMissingWeight(i);
1032  }
1033  } else {
1034  weightChoice->psWeightsDoc = "dummy PS weight (1.0) ";
1035  }
1036 
1037  weightChoice->scaleWeightIDs.clear();
1038  weightChoice->pdfWeightIDs.clear();
1039 
1040  std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
1041  std::stringstream scaleDoc;
1042  scaleDoc << "LHE scale variation weights (w_var / w_nominal); ";
1043  for (unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
1044  const auto& sw = scaleVariationIDs[isw];
1045  if (isw)
1046  scaleDoc << "; ";
1047  scaleDoc << "[" << isw << "] is " << sw.label;
1048  weightChoice->scaleWeightIDs.push_back(std::atoi(sw.wid.c_str()));
1049  }
1050  if (!scaleVariationIDs.empty())
1051  weightChoice->scaleWeightsDoc = scaleDoc.str();
1052  std::stringstream pdfDoc;
1053  pdfDoc << "LHE pdf variation weights (w_var / w_nominal) for LHA names ";
1054  bool found = false;
1055  for (const auto& pw : pdfSetWeightIDs) {
1056  if (pw.wids.size() == 1)
1057  continue; // only consider error sets
1058  for (const auto& wantedpdf : lhaNameToID_) {
1059  auto pdfname = wantedpdf.first;
1060  if (knownPDFSetsFromGenInfo_.find(pdfname) == knownPDFSetsFromGenInfo_.end())
1061  continue;
1062  uint32_t lhaid = knownPDFSetsFromGenInfo_.at(pdfname);
1063  if (pw.lhaIDs.first != lhaid)
1064  continue;
1065  pdfDoc << pdfname;
1066  for (const auto& x : pw.wids)
1067  weightChoice->pdfWeightIDs.push_back(std::atoi(x.c_str()));
1068  if (maxPdfWeights_ < pw.wids.size()) {
1069  weightChoice->pdfWeightIDs.resize(maxPdfWeights_); // drop some replicas
1070  pdfDoc << ", truncated to the first " << maxPdfWeights_ << " replicas";
1071  }
1072  weightChoice->pdfWeightsDoc = pdfDoc.str();
1073  found = true;
1074  break;
1075  }
1076  if (found)
1077  break;
1078  }
1079  }
1080  }
1081  // create an empty counter
1082  std::shared_ptr<CounterMap> globalBeginRunSummary(edm::Run const&, edm::EventSetup const&) const override {
1083  return std::make_shared<CounterMap>();
1084  }
1085  // add this stream to the summary
1087  edm::Run const&,
1088  edm::EventSetup const&,
1089  CounterMap* runCounterMap) const override {
1090  runCounterMap->merge(streamCache(id)->countermap);
1091  }
1092  // nothing to do per se
1093  void globalEndRunSummary(edm::Run const&, edm::EventSetup const&, CounterMap* runCounterMap) const override {}
1094  // write the total to the run
1095  void globalEndRunProduce(edm::Run& iRun, edm::EventSetup const&, CounterMap const* runCounterMap) const override {
1096  auto out = std::make_unique<nanoaod::MergeableCounterTable>();
1097 
1098  for (const auto& x : runCounterMap->countermap) {
1099  auto runCounter = &(x.second);
1100  std::string label = (!x.first.empty()) ? (std::string("_") + x.first) : "";
1101  std::string doclabel = (!x.first.empty()) ? (std::string(", for model label ") + x.first) : "";
1102 
1103  out->addInt("genEventCount" + label, "event count" + doclabel, runCounter->num);
1104  out->addFloat("genEventSumw" + label, "sum of gen weights" + doclabel, runCounter->sumw);
1105  out->addFloat("genEventSumw2" + label, "sum of gen (weight^2)" + doclabel, runCounter->sumw2);
1106 
1107  double norm = runCounter->sumw ? 1.0 / runCounter->sumw : 1;
1108  auto sumScales = runCounter->sumScale;
1109  for (auto& val : sumScales)
1110  val *= norm;
1111  out->addVFloatWithNorm("LHEScaleSumw" + label,
1112  "Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw" + doclabel,
1113  sumScales,
1114  runCounter->sumw);
1115  auto sumPDFs = runCounter->sumPDF;
1116  for (auto& val : sumPDFs)
1117  val *= norm;
1118  out->addVFloatWithNorm("LHEPdfSumw" + label,
1119  "Sum of genEventWeight * LHEPdfWeight[i], divided by genEventSumw" + doclabel,
1120  sumPDFs,
1121  runCounter->sumw);
1122  if (!runCounter->sumRwgt.empty()) {
1123  auto sumRwgts = runCounter->sumRwgt;
1124  for (auto& val : sumRwgts)
1125  val *= norm;
1126  out->addVFloatWithNorm("LHEReweightingSumw" + label,
1127  "Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw" + doclabel,
1128  sumRwgts,
1129  runCounter->sumw);
1130  }
1131  if (!runCounter->sumNamed.empty()) { // it could be empty if there's no LHE info in the sample
1132  for (unsigned int i = 0, n = namedWeightLabels_.size(); i < n; ++i) {
1133  out->addFloatWithNorm(
1134  "LHESumw_" + namedWeightLabels_[i] + label,
1135  "Sum of genEventWeight * LHEWeight_" + namedWeightLabels_[i] + ", divided by genEventSumw" + doclabel,
1136  runCounter->sumNamed[i] * norm,
1137  runCounter->sumw);
1138  }
1139  }
1140  }
1141  iRun.put(std::move(out));
1142  }
1143  // nothing to do here
1144  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}
1145 
1148  desc.add<edm::InputTag>("genEvent", edm::InputTag("generator"))
1149  ->setComment("tag for the GenEventInfoProduct, to get the main weight");
1150  desc.add<edm::InputTag>("genLumiInfoHeader", edm::InputTag("generator"))
1151  ->setComment("tag for the GenLumiInfoProduct, to get the model string");
1152  desc.add<std::vector<edm::InputTag>>("lheInfo", std::vector<edm::InputTag>{{"externalLHEProducer"}, {"source"}})
1153  ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
1154 
1156  prefpdf.add<std::string>("name");
1157  prefpdf.add<uint32_t>("lhaid");
1158  desc.addVPSet("preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())
1159  ->setComment(
1160  "LHA PDF Ids of the preferred PDF sets, in order of preference (the first matching one will be used)");
1161  desc.add<std::vector<std::string>>("namedWeightIDs")->setComment("set of LHA weight IDs for named LHE weights");
1162  desc.add<std::vector<std::string>>("namedWeightLabels")
1163  ->setComment("output names for the namedWeightIDs (in the same order)");
1164  desc.add<int32_t>("lheWeightPrecision")->setComment("Number of bits in the mantissa for LHE weights");
1165  desc.add<uint32_t>("maxPdfWeights")->setComment("Maximum number of PDF weights to save (to crop NN replicas)");
1166  desc.add<bool>("keepAllPSWeights")->setComment("Store all PS weights found");
1167  desc.addOptionalUntracked<bool>("debug")->setComment("dump out all LHE information for one event");
1168  descriptions.add("genWeightsTable", desc);
1169  }
1170 
1171 protected:
1173  const std::vector<edm::InputTag> lheLabel_;
1174  const std::vector<edm::EDGetTokenT<LHEEventProduct>> lheTag_;
1175  const std::vector<edm::EDGetTokenT<LHERunInfoProduct>> lheRunTag_;
1177 
1178  std::vector<uint32_t> preferredPDFLHAIDs_;
1179  std::unordered_map<std::string, uint32_t> lhaNameToID_;
1180  std::vector<std::string> namedWeightIDs_;
1181  std::vector<std::string> namedWeightLabels_;
1183  unsigned int maxPdfWeights_;
1185 
1186  mutable std::atomic<bool> debug_, debugRun_, hasIssuedWarning_, psWeightWarning_;
1187 };
1188 
const std::vector< edm::EDGetTokenT< LHERunInfoProduct > > lheRunTag_
double originalXWGTUP() const
T getParameter(std::string const &) const
bool getByLabel(std::string const &label, Handle< PROD > &result) const
Definition: Run.h:280
void setComment(std::string const &value)
void setPSWeightInfo(const std::vector< double > &genWeights, const DynamicWeightChoiceGenInfo *genWeightChoice, std::vector< double > &wPS, std::string &psWeightDocStr) const
std::unique_ptr< LumiCacheInfoHolder > beginStream(edm::StreamID) const override
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:125
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
void streamBeginLuminosityBlock(edm::StreamID id, edm::LuminosityBlock const &lumiBlock, edm::EventSetup const &eventSetup) const override
const double w
Definition: UKUtility.cc:23
def copy(args, dbName)
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:517
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const edm::EDGetTokenT< GenEventInfoProduct > genTag_
const std::vector< std::string > & weightNames() const
void fillLHEPdfWeightTablesFromGenInfo(Counter *counter, const DynamicWeightChoiceGenInfo *weightChoice, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outScale, std::unique_ptr< nanoaod::FlatTable > &outPdf, std::unique_ptr< nanoaod::FlatTable > &outNamed, std::unique_ptr< nanoaod::FlatTable > &outPS) const
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:99
double weight() const
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:20
uint16_t size_type
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:167
void fillLHEWeightTables(Counter *counter, const DynamicWeightChoice *weightChoice, const DynamicWeightChoiceGenInfo *genWeightChoice, 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
RunIndex index() const
Definition: Run.cc:21
double f[11][100]
#define end
Definition: vmac.h:39
std::atomic< bool > psWeightWarning_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
void fillOnlyPSWeightTable(Counter *counter, const DynamicWeightChoiceGenInfo *genWeightChoice, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
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:73
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:40
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
def merge(dictlist, TELL=False)
Definition: MatrixUtil.py:194