Skip to content

Commit 15c5f5f

Browse files
authored
Merge pull request #46573 from hqucms/dev/nano-genWeightTable
[NANO] Implement a check on the number of scale variation weights in GenWeightsTableProducer
2 parents 0d46da9 + c8a0f0b commit 15c5f5f

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

PhysicsTools/NanoAOD/plugins/GenWeightsTableProducer.cc

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
262262
lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")),
263263
maxPdfWeights_(params.getParameter<uint32_t>("maxPdfWeights")),
264264
keepAllPSWeights_(params.getParameter<bool>("keepAllPSWeights")),
265+
allowedNumScaleWeights_(params.getParameter<std::vector<uint32_t>>("allowedNumScaleWeights")),
265266
debug_(params.getUntrackedParameter<bool>("debug", false)),
266267
debugRun_(debug_.load()),
267268
hasIssuedWarning_(false),
@@ -559,7 +560,7 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
559560
std::vector<ScaleVarWeight> scaleVariationIDs;
560561
std::vector<PDFSetWeights> pdfSetWeightIDs;
561562
std::vector<std::string> lheReweighingIDs;
562-
bool isFirstGroup = true;
563+
bool preScaleVariationGroup = true;
563564

564565
std::regex weightgroupmg26x("<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
565566
std::regex weightgroup("<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
@@ -591,9 +592,20 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
591592
"\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
592593
"weight>");
593594

595+
std::regex mgVerRegex(R"(VERSION\s+(\d+)\.(\d+)\.(\d+))");
596+
bool isMGVer2x = false;
597+
594598
std::regex rwgt("<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
595599
std::smatch groups;
596600
for (auto iter = lheInfo->headers_begin(), end = lheInfo->headers_end(); iter != end; ++iter) {
601+
if (iter->tag() == "MG5ProcCard") {
602+
for (const auto& line : iter->lines()) {
603+
if (std::regex_search(line, groups, mgVerRegex)) {
604+
isMGVer2x = (groups[1].str() == "2");
605+
break;
606+
}
607+
}
608+
}
597609
if (iter->tag() != "initrwgt") {
598610
if (lheDebug)
599611
std::cout << "Skipping LHE header with tag" << iter->tag() << std::endl;
@@ -620,18 +632,26 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
620632
for (unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
621633
if (lheDebug)
622634
std::cout << lines[iLine];
623-
if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
624-
std::string groupname = groups.str(2);
625-
if (ismg26x)
626-
groupname = groups.str(1);
635+
auto foundWeightGroup = std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup);
636+
if (foundWeightGroup || preScaleVariationGroup) {
637+
std::string groupname;
638+
if (foundWeightGroup) {
639+
groupname = ismg26x ? groups.str(1) : groups.str(2);
640+
} else {
641+
// rewind by one line and check later in the inner loop
642+
--iLine;
643+
}
627644
if (lheDebug)
628645
std::cout << ">>> Looks like the beginning of a weight group for '" << groupname << "'" << std::endl;
629-
if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation" || isFirstGroup) {
646+
if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation" ||
647+
preScaleVariationGroup) {
630648
if (lheDebug && groupname.find("scale_variation") != 0 && groupname != "Central scale variation")
631649
std::cout << ">>> First weight is not scale variation, but assuming is the Central Weight" << std::endl;
632650
else if (lheDebug)
633651
std::cout << ">>> Looks like scale variation for theory uncertainties" << std::endl;
634-
isFirstGroup = false;
652+
if (groupname.find("scale_variation") == 0 || groupname == "Central scale variation") {
653+
preScaleVariationGroup = false;
654+
}
635655
for (++iLine; iLine < nLines; ++iLine) {
636656
if (lheDebug) {
637657
std::cout << " " << lines[iLine];
@@ -928,7 +948,16 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
928948
break;
929949
}
930950
}
951+
// check the number of scale variations
952+
if (isMGVer2x && !allowedNumScaleWeights_.empty()) {
953+
auto it = std::find(allowedNumScaleWeights_.begin(), allowedNumScaleWeights_.end(), scaleVariationIDs.size());
954+
if (it == allowedNumScaleWeights_.end()) {
955+
throw cms::Exception("LogicError")
956+
<< "Number of scale variations found (" << scaleVariationIDs.size() << ") is invalid.";
957+
}
958+
}
931959
}
960+
932961
return weightChoice;
933962
}
934963

@@ -1171,6 +1200,9 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
11711200
desc.add<int32_t>("lheWeightPrecision")->setComment("Number of bits in the mantissa for LHE weights");
11721201
desc.add<uint32_t>("maxPdfWeights")->setComment("Maximum number of PDF weights to save (to crop NN replicas)");
11731202
desc.add<bool>("keepAllPSWeights")->setComment("Store all PS weights found");
1203+
desc.add<std::vector<uint32_t>>("allowedNumScaleWeights")
1204+
->setComment(
1205+
"Allowed numbers of scale weights parsed from the header. Empty list means any number is allowed.");
11741206
desc.addOptionalUntracked<bool>("debug")->setComment("dump out all LHE information for one event");
11751207
descriptions.add("genWeightsTable", desc);
11761208
}
@@ -1189,6 +1221,7 @@ class GenWeightsTableProducer : public edm::global::EDProducer<edm::StreamCache<
11891221
int lheWeightPrecision_;
11901222
unsigned int maxPdfWeights_;
11911223
bool keepAllPSWeights_;
1224+
std::vector<uint32_t> allowedNumScaleWeights_;
11921225

11931226
mutable std::atomic<bool> debug_, debugRun_, hasIssuedWarning_, psWeightWarning_;
11941227
};

PhysicsTools/NanoAOD/python/genWeightsTable_cfi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
lheWeightPrecision = cms.int32(14),
2525
maxPdfWeights = cms.uint32(150),
2626
keepAllPSWeights = cms.bool(False),
27+
allowedNumScaleWeights = cms.vuint32(9),
2728
debug = cms.untracked.bool(False),
2829
)
2930

0 commit comments

Comments
 (0)