15 #include "boost/algorithm/string.hpp" 18 #include <unordered_map> 25 Counter() :
num(0), sumw(0), sumw2(0), sumPDF(), sumScale(), sumRwgt(), sumNamed(), sumPS() {}
31 std::vector<long double> sumPDF, sumScale, sumRwgt, sumNamed, sumPS;
40 sumNamed.clear(), sumPS.clear();
44 void incGenOnly(
double w) {
50 void incPSOnly(
double w0,
const std::vector<double>& wPS) {
53 sumPS.resize(wPS.size(), 0);
54 for (
unsigned int i = 0,
n = wPS.size();
i <
n; ++
i)
55 sumPS[
i] += (w0 * wPS[
i]);
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) {
68 if (!wScale.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]);
76 sumPDF.resize(wPDF.size(), 0);
77 for (
unsigned int i = 0, n = wPDF.size();
i <
n; ++
i)
78 sumPDF[
i] += (w0 * wPDF[
i]);
82 sumRwgt.resize(wRwgt.size(), 0);
83 for (
unsigned int i = 0, n = wRwgt.size();
i <
n; ++
i)
84 sumRwgt[
i] += (w0 * wRwgt[
i]);
86 if (!wNamed.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]);
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];
128 std::map<std::string, Counter> countermap;
131 void merge(
const CounterMap& other) {
132 for (
const auto&
y : other.countermap)
133 countermap[
y.first].merge(
y.second);
137 for (
auto x : countermap)
141 active_el = &(countermap[
label]);
142 active_label =
label;
144 void checkLabelSet() {
146 throw cms::Exception(
"LogicError",
"Called CounterMap::get() before setting the active label\n");
159 struct DynamicWeightChoice {
162 std::vector<std::string> scaleWeightIDs;
165 std::vector<std::string> pdfWeightIDs;
168 std::vector<std::string> rwgtIDs;
174 if (match != std::string::npos) {
177 return std::stof(pre) *
std::pow(10.0
f, std::stof(post));
179 return std::stof(str);
183 struct ScaleVarWeight {
185 std::pair<float, float> scales;
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);
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; }
199 lhaIDs.second = lhaID;
201 bool maybe_add(
const std::string& wid,
unsigned int lhaID) {
202 if (lhaID == lhaIDs.second + 1) {
214 edm::RunCache<DynamicWeightChoice>,
215 edm::RunSummaryCache<CounterMap>,
216 edm::EndRunProducer> {
220 lheLabel_(params.getParameter<
std::vector<
edm::
InputTag>>(
"lheInfo")),
224 lheLabel_, [
this](
const edm::InputTag&
tag) {
return mayConsume<LHERunInfoProduct, edm::InRun>(
tag); })),
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");
247 uint32_t
lhaid = pdfps.getParameter<uint32_t>(
"lhaid");
248 preferredPDFLHAIDs_.push_back(lhaid);
250 lhaNameToID_[name +
".LHgrid"] =
lhaid;
266 auto out = std::make_unique<nanoaod::FlatTable>(1,
"genWeight",
true);
267 out->setDoc(
"generator weight");
271 std::string model_label = streamCache(
id)->getLabel();
272 auto outM = std::make_unique<std::string>((!model_label.empty()) ?
std::string(
"GenModel_") + model_label :
"");
276 std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
277 std::unique_ptr<nanoaod::FlatTable> genPSTab;
280 for (
const auto& lheTag : lheTag_) {
288 const DynamicWeightChoice* weightChoice = runCache(iEvent.
getRun().
index());
291 counter, weightChoice, weight, *lheInfo, *genInfo, lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab, genPSTab);
294 fillOnlyPSWeightTable(counter, weight, *genInfo, genPSTab);
300 if (!hasIssuedWarning_.exchange(
true)) {
301 edm::LogWarning(
"LHETablesProducer") <<
"No LHEEventProduct, so there will be no LHE Tables\n";
313 const DynamicWeightChoice* weightChoice,
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(
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;
331 std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1),
332 wNamed(namedWeightIDs_.size(), 1);
335 printf(
"Weight %+9.5f rel %+9.5f for id %s\n",
weight.wgt,
weight.wgt / w0,
weight.id.c_str());
337 auto mScale =
std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(),
weight.id);
338 if (mScale != scaleWeightIDs.end())
339 wScale[mScale - scaleWeightIDs.begin()] =
weight.wgt / w0;
341 auto mPDF =
std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(),
weight.id);
342 if (mPDF != pdfWeightIDs.end())
343 wPDF[mPDF - pdfWeightIDs.begin()] =
weight.wgt / w0;
345 auto mRwgt =
std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(),
weight.id);
346 if (mRwgt != rwgtWeightIDs.end())
347 wRwgt[mRwgt - rwgtWeightIDs.begin()] =
weight.wgt / w0;
349 auto mNamed =
std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(),
weight.id);
350 if (mNamed != namedWeightIDs_.end())
351 wNamed[mNamed - namedWeightIDs_.begin()] =
weight.wgt / w0;
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;
362 outPS->addColumn<
float>(
"",
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_);
371 outScale->addColumn<
float>(
375 outPdf->addColumn<
float>(
379 outRwgt->addColumn<
float>(
383 outNamed->addColumnValue<
float>(
"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],
390 "LHE weight for id " + namedWeightIDs_[
i] +
", relative to nominal",
392 lheWeightPrecision_);
395 counter->incLHE(genWeight, wScale, wPDF, wRwgt, wNamed, wPS);
401 std::unique_ptr<nanoaod::FlatTable>& outPS)
const {
402 int vectorSize = (genProd.
weights().size() == 14 || genProd.
weights().size() == 46) ? 4 : 1;
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;
412 outPS->addColumn<
float>(
"",
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_);
420 counter->incGenOnly(genWeight);
421 counter->incPSOnly(genWeight, wPS);
428 bool lheDebug = debugRun_.exchange(
430 auto weightChoice = std::make_shared<DynamicWeightChoice>();
434 for (
const auto& lheLabel : lheLabel_) {
441 std::vector<ScaleVarWeight> scaleVariationIDs;
442 std::vector<PDFSetWeights> pdfSetWeightIDs;
443 std::vector<std::string> lheReweighingIDs;
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>");
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>");
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.*)?</" 462 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
465 if (iter->tag() !=
"initrwgt") {
467 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
471 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
472 std::vector<std::string>
lines = iter->lines();
473 bool missed_weightgroup =
475 bool ismg26x =
false;
476 for (
unsigned int iLine = 0,
nLines = lines.size(); iLine <
nLines;
478 boost::replace_all(lines[iLine],
"<",
"<");
479 boost::replace_all(lines[iLine],
">",
">");
480 if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
484 for (
unsigned int iLine = 0, nLines = lines.size(); iLine <
nLines; ++iLine) {
487 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
490 groupname = groups.str(1);
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") {
495 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
496 for (++iLine; iLine <
nLines; ++iLine) {
499 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
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)) {
506 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
507 if (!missed_weightgroup) {
510 missed_weightgroup =
false;
511 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
513 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 517 missed_weightgroup =
true;
522 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
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) {
528 if (std::regex_search(lines[iLine], groups, pdfw)) {
529 unsigned int lhaID = std::stoi(groups.str(2));
531 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
533 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
534 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
536 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
538 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
539 if (!missed_weightgroup) {
542 missed_weightgroup =
false;
543 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
545 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 549 missed_weightgroup =
true;
554 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
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) {
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));
565 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
567 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
568 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
570 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
573 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
575 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
576 if (!missed_weightgroup) {
579 missed_weightgroup =
false;
580 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
582 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 586 missed_weightgroup =
true;
591 }
else if (lhaNameToID_.find(groupname) != lhaNameToID_.end()) {
593 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
594 unsigned int firstLhaID = lhaNameToID_.find(groupname)->second;
596 for (++iLine; iLine <
nLines; ++iLine) {
599 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
600 unsigned int member = 0;
602 member = std::stoi(groups.str(2));
604 if (!groups.str(4).empty()) {
605 member = std::stoi(groups.str(4));
608 unsigned int lhaID = member + firstLhaID;
610 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
614 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
617 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
619 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
621 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
622 if (!missed_weightgroup) {
625 missed_weightgroup =
false;
626 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
628 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 632 missed_weightgroup =
true;
638 for (++iLine; iLine <
nLines; ++iLine) {
641 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
643 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
644 if (!missed_weightgroup) {
647 missed_weightgroup =
false;
648 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
650 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 654 missed_weightgroup =
true;
660 }
else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
662 if (groupname ==
"mg_reweighting") {
664 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
665 for (++iLine; iLine <
nLines; ++iLine) {
668 if (std::regex_search(lines[iLine], groups, rwgt)) {
671 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
672 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
674 lheReweighingIDs.emplace_back(rwgtID);
676 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
678 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
679 if (!missed_weightgroup) {
682 missed_weightgroup =
false;
683 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
685 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 689 missed_weightgroup =
true;
700 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
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];
709 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
710 weightChoice->scaleWeightIDs.push_back(sw.wid);
712 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
718 if (!scaleVariationIDs.empty())
719 weightChoice->scaleWeightsDoc = scaleDoc.str();
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",
729 pw.wids.front().c_str());
734 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
736 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
738 std::stringstream pdfDoc;
739 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
741 for (uint32_t
lhaid : preferredPDFLHAIDs_) {
742 for (
const auto& pw : pdfSetWeightIDs) {
743 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
745 if (pw.wids.size() == 1)
747 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
748 weightChoice->pdfWeightIDs = pw.wids;
749 if (maxPdfWeights_ < pw.wids.size()) {
750 weightChoice->pdfWeightIDs.resize(maxPdfWeights_);
751 pdfDoc <<
", truncated to the first " << maxPdfWeights_ <<
" replicas";
753 weightChoice->pdfWeightsDoc = pdfDoc.str();
769 streamCache(
id)->clear();
774 auto counterMap = streamCache(
id);
776 lumiBlock.
getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
777 if (!genLumiInfoHead.
isValid())
779 <<
"No GenLumiInfoHeader product found, will not fill generator model string.\n";
784 return std::make_shared<CounterMap>();
790 CounterMap* runCounterMap)
const override {
791 runCounterMap->merge(*streamCache(
id));
797 auto out = std::make_unique<nanoaod::MergeableCounterTable>();
799 for (
auto x : runCounterMap->countermap) {
800 auto runCounter = &(
x.second);
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);
808 double norm = runCounter->sumw ? 1.0 / runCounter->sumw : 1;
809 auto sumScales = runCounter->sumScale;
810 for (
auto&
val : sumScales)
812 out->addVFloat(
"LHEScaleSumw" + label,
813 "Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw" + doclabel,
815 auto sumPDFs = runCounter->sumPDF;
816 for (
auto&
val : sumPDFs)
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)
824 out->addVFloat(
"LHEReweightingSumw" + label,
825 "Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw" + doclabel,
828 if (!runCounter->sumNamed.empty()) {
829 for (
unsigned int i = 0, n = namedWeightLabels_.size();
i <
n; ++
i) {
831 "LHESumw_" + namedWeightLabels_[
i] + label,
832 "Sum of genEventWeight * LHEWeight_" + namedWeightLabels_[
i] +
", divided by genEventSumw" + doclabel,
833 runCounter->sumNamed[
i] * norm);
845 ->setComment(
"tag for the GenEventInfoProduct, to get the main weight");
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)");
853 prefpdf.
add<uint32_t>(
"lhaid");
854 desc.
addVPSet(
"preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())
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);
869 const std::vector<edm::EDGetTokenT<LHEEventProduct>>
lheTag_;
870 const std::vector<edm::EDGetTokenT<LHERunInfoProduct>>
lheRunTag_;
const std::vector< edm::EDGetTokenT< LHERunInfoProduct > > lheRunTag_
double originalXWGTUP() const
bool getByLabel(std::string const &label, Handle< PROD > &result) const
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.
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
void globalEndRun(edm::Run const &, edm::EventSetup const &) const override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
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
headers_const_iterator headers_end() const
Run const & getRun() 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)
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
std::function< unsigned int(align::ID)> Counter
std::shared_ptr< CounterMap > globalBeginRunSummary(edm::Run const &, edm::EventSetup const &) const override
#define DEFINE_FWK_MODULE(type)
headers_const_iterator headers_begin() const
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
void fillOnlyPSWeightTable(Counter *counter, double genWeight, const GenEventInfoProduct &genProd, std::unique_ptr< nanoaod::FlatTable > &outPS) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
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 >
std::vector< std::string > namedWeightLabels_
~GenWeightsTableProducer() override
GenWeightsTableProducer(edm::ParameterSet const ¶ms)
void put(std::unique_ptr< PROD > product)
Put a new product.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
unsigned int maxPdfWeights_
const std::vector< edm::EDGetTokenT< LHEEventProduct > > lheTag_
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
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_
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.
std::vector< std::string > namedWeightIDs_
const edm::EDGetTokenT< GenLumiInfoHeader > genLumiInfoHeadTag_
Power< A, B >::type pow(const A &a, const B &b)
def merge(dictlist, TELL=False)