15 #include "boost/algorithm/string.hpp" 18 #include <unordered_map> 26 num(0), sumw(0), sumw2(0), sumPDF(), sumScale(), sumRwgt(), sumNamed(), sumPS() {}
32 std::vector<long double> sumPDF, sumScale, sumRwgt, sumNamed, sumPS;
35 num = 0; sumw = 0; sumw2 = 0;
36 sumPDF.clear(); sumScale.clear(); sumRwgt.clear(); sumNamed.clear(), sumPS.clear();
40 void incGenOnly(
double w) {
41 num++; sumw +=
w; sumw2 += (w*
w);
44 void incPSOnly(
double w0,
const std::vector<double> & wPS) {
46 if (sumPS.empty()) sumPS.resize(wPS.size(), 0);
47 for (
unsigned int i = 0,
n = wPS.size();
i <
n; ++
i) sumPS[
i] += (w0 * wPS[
i]);
51 void incLHE(
double w0,
const std::vector<double> & wScale,
const std::vector<double> & wPDF,
const std::vector<double> & wRwgt,
const std::vector<double> & wNamed,
const std::vector<double> & wPS) {
55 if (!wScale.empty()) {
56 if (sumScale.empty()) sumScale.resize(wScale.size(), 0);
57 for (
unsigned int i = 0,
n = wScale.size();
i <
n; ++
i) sumScale[
i] += (w0 * wScale[
i]);
60 if (sumPDF.empty()) sumPDF.resize(wPDF.size(), 0);
61 for (
unsigned int i = 0, n = wPDF.size();
i <
n; ++
i) sumPDF[
i] += (w0 * wPDF[
i]);
64 if (sumRwgt.empty()) sumRwgt.resize(wRwgt.size(), 0);
65 for (
unsigned int i = 0, n = wRwgt.size();
i <
n; ++
i) sumRwgt[
i] += (w0 * wRwgt[
i]);
67 if (!wNamed.empty()) {
68 if (sumNamed.empty()) sumNamed.resize(wNamed.size(), 0);
69 for (
unsigned int i = 0, n = wNamed.size();
i <
n; ++
i) sumNamed[
i] += (w0 * wNamed[
i]);
75 num += other.num; sumw += other.sumw; sumw2 += other.sumw2;
76 if (sumScale.empty() && !other.sumScale.empty()) sumScale.resize(other.sumScale.size(),0);
77 if (sumPDF.empty() && !other.sumPDF.empty()) sumPDF.resize(other.sumPDF.size(),0);
78 if (sumRwgt.empty() && !other.sumRwgt.empty()) sumRwgt.resize(other.sumRwgt.size(),0);
79 if (sumNamed.empty() && !other.sumNamed.empty()) sumNamed.resize(other.sumNamed.size(),0);
80 if (sumPS.empty() && !other.sumPS.empty()) sumPS.resize(other.sumPS.size(),0);
81 if (!other.sumScale.empty())
for (
unsigned int i = 0, n = sumScale.size();
i <
n; ++
i) sumScale[
i] += other.sumScale[
i];
82 if (!other.sumPDF.empty())
for (
unsigned int i = 0, n = sumPDF.size();
i <
n; ++
i) sumPDF[
i] += other.sumPDF[
i];
83 if (!other.sumRwgt.empty())
for (
unsigned int i = 0, n = sumRwgt.size();
i <
n; ++
i) sumRwgt[
i] += other.sumRwgt[
i];
84 if (!other.sumNamed.empty())
for (
unsigned int i = 0, n = sumNamed.size();
i <
n; ++
i) sumNamed[
i] += other.sumNamed[
i];
85 if (!other.sumPS.empty())
for (
unsigned int i = 0, n = sumPS.size();
i <
n; ++
i) sumPS[
i] += other.sumPS[
i];
90 std::map<std::string, Counter> countermap;
93 void merge(
const CounterMap & other) {
94 for (
const auto &
y : other.countermap) countermap[
y.first].merge(
y.second);
97 void clear() {
for (
auto x : countermap)
x.second.clear();}
99 void checkLabelSet() {
if (!active_el)
throw cms::Exception(
"LogicError",
"Called CounterMap::get() before setting the active label\n"); }
100 Counter*
get() { checkLabelSet();
return active_el; }
101 std::string& getLabel() { checkLabelSet();
return active_label; }
105 struct DynamicWeightChoice {
108 std::vector<std::string> scaleWeightIDs;
111 std::vector<std::string> pdfWeightIDs;
114 std::vector<std::string> rwgtIDs;
120 if (match != std::string::npos) {
123 return std::stof(pre) *
std::pow(10.0
f, std::stof(post));
125 return std::stof(str);
129 struct ScaleVarWeight {
131 std::pair<float,float> scales;
133 wid(id), label(text), scales(stof_fortrancomp(muR), stof_fortrancomp(muF)) {}
134 bool operator<(
const ScaleVarWeight & other) {
return (scales == other.scales ? wid < other.wid : scales < other.scales); }
136 struct PDFSetWeights {
137 std::vector<std::string> wids;
138 std::pair<unsigned int,unsigned int> lhaIDs;
139 PDFSetWeights(
const std::string & wid,
unsigned int lhaID) : wids(1,wid), lhaIDs(lhaID,lhaID) {}
140 bool operator<(
const PDFSetWeights & other)
const {
return lhaIDs < other.lhaIDs; }
143 lhaIDs.second = lhaID;
145 bool maybe_add(
const std::string & wid,
unsigned int lhaID) {
146 if (lhaID == lhaIDs.second+1) {
161 lheLabel_(params.getParameter<
std::vector<
edm::InputTag>>(
"lheInfo")),
164 genLumiInfoHeadTag_(mayConsume<GenLumiInfoHeader,edm::InLumi>(params.getParameter<
edm::InputTag>(
"genLumiInfoHeader"))),
165 namedWeightIDs_(params.getParameter<std::vector<std::string>>(
"namedWeightIDs")),
166 namedWeightLabels_(params.getParameter<std::vector<std::string>>(
"namedWeightLabels")),
167 lheWeightPrecision_(params.getParameter<int32_t>(
"lheWeightPrecision")),
168 maxPdfWeights_(params.getParameter<uint32_t>(
"maxPdfWeights")),
169 debug_(params.getUntrackedParameter<
bool>(
"debug",
false)), debugRun_(debug_.load()),
170 hasIssuedWarning_(
false)
172 produces<nanoaod::FlatTable>();
173 produces<std::string>(
"genModel");
174 produces<nanoaod::FlatTable>(
"LHEScale");
175 produces<nanoaod::FlatTable>(
"LHEPdf");
176 produces<nanoaod::FlatTable>(
"LHEReweighting");
177 produces<nanoaod::FlatTable>(
"LHENamed");
178 produces<nanoaod::FlatTable>(
"PS");
179 produces<nanoaod::MergeableCounterTable,edm::Transition::EndRun>();
180 if (namedWeightIDs_.size() != namedWeightLabels_.size()) {
181 throw cms::Exception(
"Configuration",
"Size mismatch between namedWeightIDs & namedWeightLabels");
185 uint32_t
lhaid = pdfps.getParameter<uint32_t>(
"lhaid");
186 preferredPDFLHAIDs_.push_back(lhaid);
188 lhaNameToID_[name+
".LHgrid"] =
lhaid;
204 auto out = std::make_unique<nanoaod::FlatTable>(1,
"genWeight",
true);
205 out->setDoc(
"generator weight");
209 std::string model_label = streamCache(
id)->getLabel();
210 auto outM = std::make_unique<std::string>((!model_label.empty()) ?
std::string(
"GenModel_") + model_label :
"");
214 std::unique_ptr<nanoaod::FlatTable> lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab;
215 std::unique_ptr<nanoaod::FlatTable> genPSTab;
218 for (
const auto & lheTag: lheTag_) {
226 const DynamicWeightChoice * weightChoice = runCache(iEvent.
getRun().
index());
228 fillLHEWeightTables(counter, weightChoice, weight, *lheInfo, *genInfo, lheScaleTab, lhePdfTab, lheRwgtTab, lheNamedTab, genPSTab);
231 fillOnlyPSWeightTable(counter, weight, *genInfo, genPSTab);
237 if (!hasIssuedWarning_.exchange(
true)) {
238 edm::LogWarning(
"LHETablesProducer") <<
"No LHEEventProduct, so there will be no LHE Tables\n";
251 const DynamicWeightChoice * weightChoice,
255 std::unique_ptr<nanoaod::FlatTable> & outScale,
256 std::unique_ptr<nanoaod::FlatTable> & outPdf,
257 std::unique_ptr<nanoaod::FlatTable> & outRwgt,
258 std::unique_ptr<nanoaod::FlatTable> & outNamed,
259 std::unique_ptr<nanoaod::FlatTable> & outPS )
const 261 bool lheDebug = debug_.exchange(
false);
263 const std::vector<std::string> & scaleWeightIDs = weightChoice->scaleWeightIDs;
264 const std::vector<std::string> & pdfWeightIDs = weightChoice->pdfWeightIDs;
265 const std::vector<std::string> & rwgtWeightIDs = weightChoice->rwgtIDs;
269 std::vector<double> wScale(scaleWeightIDs.size(), 1), wPDF(pdfWeightIDs.size(), 1), wRwgt(rwgtWeightIDs.size(), 1), wNamed(namedWeightIDs_.size(), 1);
271 if (lheDebug) printf(
"Weight %+9.5f rel %+9.5f for id %s\n",
weight.wgt,
weight.wgt/w0,
weight.id.c_str());
273 auto mScale =
std::find(scaleWeightIDs.begin(), scaleWeightIDs.end(),
weight.id);
274 if (mScale != scaleWeightIDs.end()) wScale[mScale-scaleWeightIDs.begin()] =
weight.wgt/w0;
276 auto mPDF =
std::find(pdfWeightIDs.begin(), pdfWeightIDs.end(),
weight.id);
277 if (mPDF != pdfWeightIDs.end()) wPDF[mPDF-pdfWeightIDs.begin()] =
weight.wgt/w0;
279 auto mRwgt =
std::find(rwgtWeightIDs.begin(), rwgtWeightIDs.end(),
weight.id);
280 if (mRwgt != rwgtWeightIDs.end()) wRwgt[mRwgt-rwgtWeightIDs.begin()] =
weight.wgt/w0;
282 auto mNamed =
std::find(namedWeightIDs_.begin(), namedWeightIDs_.end(),
weight.id);
283 if (mNamed != namedWeightIDs_.end()) wNamed[mNamed-namedWeightIDs_.begin()] =
weight.wgt/w0;
286 int vectorSize = (genProd.
weights().size() == 14 || genProd.
weights().size() == 46) ? 4 : 1;
287 std::vector<double> wPS(vectorSize, 1);
288 if (vectorSize > 1 ) {
289 for (
unsigned int i=6;
i<10;
i++){
294 outPS->addColumn<
float>(
"", wPS, vectorSize > 1 ?
"PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 " :
"dummy PS weight (1.0) ",
nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
307 for (
unsigned int i = 0, n = wNamed.size();
i <
n; ++
i) {
308 outNamed->addColumnValue<
float>(namedWeightLabels_[
i], wNamed[
i],
"LHE weight for id "+namedWeightIDs_[
i]+
", relative to nominal",
nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
311 counter->incLHE(genWeight, wScale, wPDF, wRwgt, wNamed, wPS);
318 std::unique_ptr<nanoaod::FlatTable> & outPS )
const 320 int vectorSize = (genProd.
weights().size() == 14 || genProd.
weights().size() == 46) ? 4 : 1;
322 std::vector<double> wPS(vectorSize, 1);
323 if (vectorSize > 1 ){
324 for (
unsigned int i=6;
i<10;
i++){
325 wPS[
i-6] = (genProd.
weights()[
i])/genWeight;
330 outPS->addColumn<
float>(
"", wPS, vectorSize > 1 ?
"PS weights (w_var / w_nominal); [0] is ISR=0.5 FSR=1; [1] is ISR=1 FSR=0.5; [2] is ISR=2 FSR=1; [3] is ISR=1 FSR=2 " :
"dummy PS weight (1.0) " ,
nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
332 counter->incGenOnly(genWeight);
333 counter->incPSOnly(genWeight,wPS);
342 bool lheDebug = debugRun_.exchange(
false);
343 auto weightChoice = std::make_shared<DynamicWeightChoice>();
347 for (
const auto & lheLabel: lheLabel_) {
354 std::vector<ScaleVarWeight> scaleVariationIDs;
355 std::vector<PDFSetWeights> pdfSetWeightIDs;
356 std::vector<std::string> lheReweighingIDs;
358 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
359 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
360 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
361 std::regex endweightgroup(
"</weightgroup>");
362 std::regex scalewmg26x(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
363 std::regex scalew(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
364 std::regex pdfw(
"<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
365 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
366 std::regex pdfwmg26x(
"<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</weight>");
367 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
370 if (iter->tag() !=
"initrwgt") {
371 if (lheDebug)
std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
374 if (lheDebug)
std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
375 std::vector<std::string>
lines = iter->lines();
376 bool missed_weightgroup=
false;
378 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
379 boost::replace_all(lines[iLine],
"<",
"<");
380 boost::replace_all(lines[iLine],
">",
">");
381 if(std::regex_search(lines[iLine],groups,weightgroupmg26x)){
385 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
387 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup) ) {
389 if (ismg26x) groupname = groups.str(1);
390 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
391 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
392 if (lheDebug)
std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
393 for ( ++iLine; iLine < nLines; ++iLine) {
394 if (lheDebug)
std::cout <<
" " << lines[iLine];
395 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
396 if (lheDebug)
std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " << groups[4].str() <<
" , " << groups[5].str() << std::endl;
397 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
398 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
399 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
400 if (!missed_weightgroup){
402 }
else missed_weightgroup=
false;
403 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
404 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
405 if (ismg26x) missed_weightgroup=
true;
410 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
411 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
412 for ( ++iLine; iLine < nLines; ++iLine) {
413 if (lheDebug)
std::cout <<
" " << lines[iLine];
414 if (std::regex_search(lines[iLine], groups, pdfw)) {
415 unsigned int lhaID = std::stoi(groups.str(2));
416 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
417 if (pdfSetWeightIDs.empty() || ! pdfSetWeightIDs.back().maybe_add(groups.str(1),lhaID)) {
418 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
420 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
421 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
422 if (!missed_weightgroup){
424 }
else missed_weightgroup=
false;
425 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
426 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
427 if (ismg26x) missed_weightgroup=
true;
432 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
433 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
434 unsigned int lastid = 0;
435 for ( ++iLine; iLine < nLines; ++iLine) {
436 if (lheDebug)
std::cout <<
" " << lines[iLine];
437 if (std::regex_search(lines[iLine], groups, pdfw)) {
438 unsigned int id = std::stoi(groups.str(1));
439 unsigned int lhaID = std::stoi(groups.str(2));
440 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
441 if (
id != (lastid+1) || pdfSetWeightIDs.empty()) {
442 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
444 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
447 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
448 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
449 if(!missed_weightgroup) {
451 }
else missed_weightgroup=
false;
452 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
453 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
454 if (ismg26x) missed_weightgroup=
true;
459 }
else if (lhaNameToID_.find(groupname) != lhaNameToID_.end()) {
460 if (lheDebug)
std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
461 unsigned int firstLhaID = lhaNameToID_.find(groupname)->second;
463 for ( ++iLine; iLine < nLines; ++iLine) {
464 if (lheDebug)
std::cout <<
" " << lines[iLine];
465 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
466 unsigned int member = 0;
468 member = std::stoi(groups.str(2));
470 if (!groups.str(4).empty()){
471 member = std::stoi(groups.str(4));
474 unsigned int lhaID = member+firstLhaID;
475 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID << std::endl;
478 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
481 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
483 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
484 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
485 if (!missed_weightgroup) {
487 }
else missed_weightgroup=
false;
488 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
489 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
490 if (ismg26x) missed_weightgroup=
true;
496 for ( ++iLine; iLine < nLines; ++iLine) {
497 if (lheDebug)
std::cout <<
" " << lines[iLine];
498 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
499 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
500 if (!missed_weightgroup){
502 }
else missed_weightgroup=
false;
503 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
504 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
505 if (ismg26x) missed_weightgroup=
true;
511 }
else if(std::regex_search(lines[iLine], groups, weightgroupRwgt) ) {
513 if (groupname ==
"mg_reweighting") {
514 if (lheDebug)
std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
515 for ( ++iLine; iLine < nLines; ++iLine) {
516 if (lheDebug)
std::cout <<
" " << lines[iLine];
517 if (std::regex_search(lines[iLine], groups, rwgt)) {
519 if (lheDebug)
std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
520 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
522 lheReweighingIDs.emplace_back(rwgtID);
524 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
525 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
526 if (!missed_weightgroup){
528 }
else missed_weightgroup=
false;
529 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
530 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
531 if (ismg26x) missed_weightgroup=
true;
542 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
543 if (lheDebug)
std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
544 std::stringstream scaleDoc; scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
545 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
546 const auto & sw = scaleVariationIDs[isw];
547 if (isw) scaleDoc <<
"; ";
548 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
549 weightChoice->scaleWeightIDs.push_back(sw.wid);
550 if (lheDebug) printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n", sw.wid.c_str(), sw.scales.first, sw.scales.second, sw.label.c_str());
552 if (!scaleVariationIDs.empty()) weightChoice->scaleWeightsDoc = scaleDoc.str();
556 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
557 for (
const auto & pw : pdfSetWeightIDs) printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n", pw.lhaIDs.first, pw.lhaIDs.second, pw.wids.size(), pw.wids.front().c_str());
562 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
564 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
566 std::stringstream pdfDoc; pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
568 for (uint32_t
lhaid : preferredPDFLHAIDs_) {
569 for (
const auto & pw : pdfSetWeightIDs) {
570 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid+1))
continue;
571 if (pw.wids.size() == 1)
continue;
572 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
573 weightChoice->pdfWeightIDs = pw.wids;
574 if (maxPdfWeights_ < pw.wids.size()) {
575 weightChoice->pdfWeightIDs.resize(maxPdfWeights_);
576 pdfDoc <<
", truncated to the first " << maxPdfWeights_ <<
" replicas";
578 weightChoice->pdfWeightsDoc = pdfDoc.str();
591 return std::make_unique<CounterMap>();
595 streamCache(
id)->clear();
598 auto counterMap = streamCache(
id);
600 lumiBlock.
getByToken(genLumiInfoHeadTag_,genLumiInfoHead);
601 if (!genLumiInfoHead.
isValid())
edm::LogWarning(
"LHETablesProducer") <<
"No GenLumiInfoHeader product found, will not fill generator model string.\n";
606 return std::make_shared<CounterMap>();
610 runCounterMap->merge(*streamCache(
id));
617 auto out = std::make_unique<nanoaod::MergeableCounterTable>();
619 for (
auto x : runCounterMap->countermap) {
621 auto runCounter = &(
x.second);
625 out->addInt(
"genEventCount"+label,
"event count"+doclabel, runCounter->num);
626 out->addFloat(
"genEventSumw"+label,
"sum of gen weights"+doclabel, runCounter->sumw);
627 out->addFloat(
"genEventSumw2"+label,
"sum of gen (weight^2)"+doclabel, runCounter->sumw2);
629 double norm = runCounter->sumw ? 1.0/runCounter->sumw : 1;
630 auto sumScales = runCounter->sumScale;
for (
auto &
val : sumScales)
val *= norm;
631 out->addVFloat(
"LHEScaleSumw"+label,
"Sum of genEventWeight * LHEScaleWeight[i], divided by genEventSumw"+doclabel, sumScales);
632 auto sumPDFs = runCounter->sumPDF;
for (
auto &
val : sumPDFs)
val *= norm;
633 out->addVFloat(
"LHEPdfSumw"+label,
"Sum of genEventWeight * LHEPdfWeight[i], divided by genEventSumw"+doclabel, sumPDFs);
634 if (!runCounter->sumRwgt.empty()) {
635 auto sumRwgts = runCounter->sumRwgt;
for (
auto &
val : sumRwgts)
val *= norm;
636 out->addVFloat(
"LHEReweightingSumw"+label,
"Sum of genEventWeight * LHEReweightingWeight[i], divided by genEventSumw"+doclabel, sumRwgts);
638 if (!runCounter->sumNamed.empty()) {
639 for (
unsigned int i = 0, n = namedWeightLabels_.size();
i <
n; ++
i) {
640 out->addFloat(
"LHESumw_"+namedWeightLabels_[
i]+label,
"Sum of genEventWeight * LHEWeight_"+namedWeightLabels_[
i]+
", divided by genEventSumw"+doclabel, runCounter->sumNamed[
i] * norm);
653 desc.
add<
edm::InputTag>(
"genLumiInfoHeader",
edm::InputTag(
"generator"))->setComment(
"tag for the GenLumiInfoProduct, to get the model string");
654 desc.
add<std::vector<edm::InputTag>>(
"lheInfo", std::vector<edm::InputTag>{{
"externalLHEProducer"},{
"source"}})->setComment(
"tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
658 prefpdf.
add<uint32_t>(
"lhaid");
659 desc.
addVPSet(
"preferredPDFs", prefpdf, std::vector<edm::ParameterSet>())->
setComment(
"LHA PDF Ids of the preferred PDF sets, in order of preference (the first matching one will be used)");
660 desc.
add<std::vector<std::string>>(
"namedWeightIDs")->setComment(
"set of LHA weight IDs for named LHE weights");
661 desc.
add<std::vector<std::string>>(
"namedWeightLabels")->setComment(
"output names for the namedWeightIDs (in the same order)");
662 desc.
add<int32_t>(
"lheWeightPrecision")->setComment(
"Number of bits in the mantissa for LHE weights");
663 desc.
add<uint32_t>(
"maxPdfWeights")->setComment(
"Maximum number of PDF weights to save (to crop NN replicas)");
664 desc.
addOptionalUntracked<
bool>(
"debug")->setComment(
"dump out all LHE information for one event");
665 descriptions.
add(
"genWeightsTable", desc);
672 const std::vector<edm::EDGetTokenT<LHEEventProduct>>
lheTag_;
673 const std::vector<edm::EDGetTokenT<LHERunInfoProduct>>
lheRunTag_;
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
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
std::unordered_map< std::string, uint32_t > lhaNameToID_
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_
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)