From 8fcc2305c2f0c46e180f9acb8e641640d0b024bb Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Sat, 11 Jan 2020 12:44:51 +0100 Subject: [PATCH] Update weighttype, read storage info from config --- .../Core/plugins/GenWeightsTestAnalyzer.cc | 4 +- .../Core/src/LHEWeightHelper.cc | 2 +- GeneratorInterface/Core/src/WeightHelper.cc | 2 +- .../LHEInterface/interface/TestWeightInfo.h | 4 +- .../LHEInterface/plugins/LHESource.cc | 4 +- .../plugins/LHEWeightsTableProducer.cc | 105 +++++++++++------- PhysicsTools/NanoAOD/python/nanogen_cff.py | 11 +- .../interface/MEParamWeightGroupInfo.h | 4 +- .../interface/PartonShowerWeightGroupInfo.h | 2 +- .../interface/PdfWeightGroupInfo.h | 6 +- .../interface/ScaleWeightGroupInfo.h | 2 +- .../interface/UnknownWeightGroupInfo.h | 6 +- .../interface/WeightGroupInfo.h | 18 ++- .../src/GenWeightInfoProduct.cc | 2 +- 14 files changed, 104 insertions(+), 68 deletions(-) diff --git a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc b/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc index aead5ad2baff4..492bdcd6a028f 100644 --- a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc @@ -422,7 +422,7 @@ GenWeightsTestAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle); // Should add a search by name function - auto allScaleWeights = lheWeightInfoHandle->weightGroupIndicesByType(gen::kScaleWeights); + auto allScaleWeights = lheWeightInfoHandle->weightGroupIndicesByType(gen::WeightType::kScaleWeights); if (allScaleWeights.size() > 0) scaleWeightsIndex_ = allScaleWeights.front(); @@ -441,7 +441,7 @@ GenWeightsTestAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, scaleWeightsOrder_.push_back(scaleWeights->muR2muF1Index()); scaleWeightsOrder_.push_back(scaleWeights->muR2muF2Index()); - auto pdfGroups = lheWeightInfoHandle->weightGroupsByType(gen::kPdfWeights); + auto pdfGroups = lheWeightInfoHandle->weightGroupsByType(gen::WeightType::kPdfWeights); auto ct14Set = std::find_if(pdfGroups.begin(), pdfGroups.end(), [] (gen::WeightGroupInfo* group) { auto pdfGroup = dynamic_cast(group); diff --git a/GeneratorInterface/Core/src/LHEWeightHelper.cc b/GeneratorInterface/Core/src/LHEWeightHelper.cc index e715e38f78fc3..55952e28210ed 100644 --- a/GeneratorInterface/Core/src/LHEWeightHelper.cc +++ b/GeneratorInterface/Core/src/LHEWeightHelper.cc @@ -155,7 +155,7 @@ namespace gen { weightGroups_.push_back(std::make_unique("Unknown")); } auto& group = weightGroups_.back(); - if (group.weightType() == gen::kScaleWeights) { + if (group.weightType() == gen::WeightType::kScaleWeights) { if (currWeightAttributeMap_["mur"].empty() || currWeightAttributeMap_["muf"].empty()) { group.setIsWellFormed(false); } diff --git a/GeneratorInterface/Core/src/WeightHelper.cc b/GeneratorInterface/Core/src/WeightHelper.cc index 0d88d2c55fb80..091feab7d6265 100644 --- a/GeneratorInterface/Core/src/WeightHelper.cc +++ b/GeneratorInterface/Core/src/WeightHelper.cc @@ -53,7 +53,7 @@ namespace gen { void WeightHelper::setGroupInfo() { auto& group = weightGroups_.back(); const std::string& name = group.name(); - if (group.weightType() == kPdfWeights) { + if (group.weightType() == WeightType::kPdfWeights) { PdfWeightGroupInfo* pdfGroup = dynamic_cast(&group); auto pdfInfo = std::find_if(pdfSetsInfo.begin(), pdfSetsInfo.end(), [name] (const PdfSetInfo& setInfo) { return setInfo.name == name; }); diff --git a/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h b/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h index 7e386bf1cd4ce..b0c52f6671077 100644 --- a/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h +++ b/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h @@ -25,7 +25,7 @@ gen::WeightGroupInfo getExampleScaleWeights() { R"( mur=0.5 muf=2 )", R"( mur=0.5 muf=0.5 )", }; - scaleInfo.setWeightType(gen::kScaleWeights); + scaleInfo.setWeightType(gen::WeightType::kScaleWeights); for (size_t i = 0; i < entries.size(); i++) { scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]); @@ -49,7 +49,7 @@ gen::WeightGroupInfo getExampleScaleWeightsOutOfOrder() { R"( mur=0.5 muf=1 )", R"( mur=0.5 muf=0.5 )", }; - scaleInfo.setWeightType(gen::kScaleWeights); + scaleInfo.setWeightType(gen::WeightType::kScaleWeights); for (size_t i = 0; i < entries.size(); i++) { scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]); diff --git a/GeneratorInterface/LHEInterface/plugins/LHESource.cc b/GeneratorInterface/LHEInterface/plugins/LHESource.cc index f045da892bb94..a29960d0a0a1b 100644 --- a/GeneratorInterface/LHEInterface/plugins/LHESource.cc +++ b/GeneratorInterface/LHEInterface/plugins/LHESource.cc @@ -128,12 +128,12 @@ void LHESource::putWeightInfoProduct(edm::RunPrincipal& iRunPrincipal) { gen::WeightGroupInfo scaleInfo( "" ); - scaleInfo.setWeightType(gen::kScaleWeights); + scaleInfo.setWeightType(gen::WeightType::kScaleWeights); gen::WeightGroupInfo cenPdfInfo( "" ); - cenPdfInfo.setWeightType(gen::kPdfWeights); + cenPdfInfo.setWeightType(gen::WeightType::kPdfWeights); product->addWeightGroupInfo(&scaleInfo); product->addWeightGroupInfo(&cenPdfInfo); diff --git a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc index 9e074c4912c74..4be52b8da8b62 100644 --- a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc +++ b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc @@ -13,6 +13,7 @@ #include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h" #include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h" #include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h" +#include "FWCore/Utilities/interface/transform.h" #include @@ -21,25 +22,23 @@ namespace { typedef std::pair, std::vector> WeightGroupsToStore; - //struct WeightGroupToStore { - // gen::WeightType type; - // std::string name; - // bool inLHE; - // gen::WeightGroupData groupInfo; - //} } // namespace class LHEWeightsTableProducer : public edm::global::EDProducer> { public: - LHEWeightsTableProducer(edm::ParameterSet const& params) - : lheInputTag_(params.getParameter("lheInfo")), + LHEWeightsTableProducer(edm::ParameterSet const& params) : lheToken_(consumes(params.getParameter("lheInfo"))), lheWeightToken_(consumes(params.getParameter("lheWeights"))), lheWeightInfoToken_(consumes(params.getParameter("lheWeights"))), genWeightToken_(consumes(params.getParameter("genWeights"))), genWeightInfoToken_(consumes(params.getParameter("genWeights"))), - //weightgroups_(params.getParameter>("weightgroups")), + weightgroups_(edm::vector_transform(params.getParameter>("weightgroups"), + [](auto& c) { return gen::WeightType(c.at(0)); } )), + maxGroupsPerType_(params.getParameter>("maxGroupsPerType")), + pdfIds_(params.getParameter>("pdfIds")), lheWeightPrecision_(params.getParameter("lheWeightPrecision")) { + if (weightgroups_.size() != maxGroupsPerType_.size()) + throw std::invalid_argument("Inputs 'weightgroups' and 'weightgroupNums' must have equal size"); produces>(); } @@ -61,35 +60,39 @@ class LHEWeightsTableProducer : public edm::global::EDProducer>& lheWeightTables, std::string entryName, + void addWeightGroupToTable(std::unique_ptr>& lheWeightTables, const char* typeName, const std::vector& weightInfos, WeightsContainer& lheWeights, double w0) const { size_t typeCount = 0; - gen::WeightType currentGroup = gen::kUnknownWeights; + gen::WeightType previousType = gen::WeightType::kUnknownWeights; + size_t index = 0; for (const auto& groupInfo : weightInfos) { + std::string entryName = typeName; gen::WeightType weightType = groupInfo.group->weightType(); + if (previousType != weightType) + typeCount = 0; std::string name = weightTypeNames_.at(weightType); - auto weights = weightType != gen::kScaleWeights ? normalizedWeights(lheWeights, groupInfo, w0) : + auto weights = weightType != gen::WeightType::kScaleWeights ? normalizedWeights(lheWeights, groupInfo, w0) : orderedScaleWeights(lheWeights, groupInfo, w0); - if (typeCount > 0) - entryName.append(std::to_string(typeCount)); entryName.append(name); entryName.append("Weight"); + if (typeCount > 0) { + entryName.append("AltSet"); + entryName.append(std::to_string(typeCount)); + } lheWeightTables->emplace_back(weights.size(), entryName, false); lheWeightTables->back().addColumn( "", weights, groupInfo.group->name(), nanoaod::FlatTable::FloatColumn, lheWeightPrecision_); typeCount++; - if (currentGroup != weightType) - typeCount = 0; - currentGroup = weightType; + previousType = weightType; } } @@ -105,26 +108,48 @@ class LHEWeightsTableProducer : public edm::global::EDProducer genWeightInfoHandle; iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle); - auto scaleGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kScaleWeights); - auto meGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kMEParamWeights); + std::unordered_map storePerType; + for (size_t i = 0; i < weightgroups_.size(); i++) + storePerType[weightgroups_.at(i)] = maxGroupsPerType_.at(i); WeightGroupsToStore weightsToStore; - weightsToStore.first.insert(weightsToStore.first.end(), scaleGroups.begin(), scaleGroups.end()); - weightsToStore.first.insert(weightsToStore.first.end(), meGroups.begin(), meGroups.end()); + for (auto weightType : gen::allGenWeightTypes) { + auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]); + weightsToStore.first.insert(weightsToStore.first.end(), lheWeights.begin(), lheWeights.end()); - for (auto lhaid : {306000, 91400, 260000}) { - if (auto pdfGroup = lheWeightInfoHandle->pdfGroupWithIndexByLHAID(lhaid)) { - weightsToStore.first.push_back(pdfGroup.value()); - break; - } + auto genWeights = weightDataPerType(genWeightInfoHandle, weightType, storePerType[weightType]); + weightsToStore.second.insert(weightsToStore.second.end(), genWeights.begin(), genWeights.end()); } - auto psGroups = genWeightInfoHandle->weightGroupsAndIndicesByType(gen::kPartonShowerWeights); - weightsToStore.second.insert(weightsToStore.second.end(), psGroups.begin(), psGroups.end()); - return std::make_shared(weightsToStore); } + std::vector weightDataPerType(edm::Handle& weightsHandle, + gen::WeightType weightType, int& maxStore) const { + std::vector group; + if (weightType == gen::WeightType::kPdfWeights) { + for (auto lhaid : pdfIds_) { + if (auto pdfGroup = weightsHandle->pdfGroupWithIndexByLHAID(lhaid)) { + group.push_back(pdfGroup.value()); + maxStore--; + if (maxStore == 0) + break; + } + } + return group; + } + + group = weightsHandle->weightGroupsAndIndicesByType(weightType); + + if (maxStore < 0 || static_cast(group.size()) < maxStore) { + // Modify size in case one type of weight is present in multiple products + maxStore -= group.size(); + return group; + } + return std::vector(group.begin(), group.begin()+maxStore); + } + + std::vector normalizedWeights(WeightsContainer& lheWeights, const gen::WeightGroupData& meGroupInfo, double w0) const { std::vector normalizedWeights; for (const auto& weight : lheWeights.at(meGroupInfo.index)) @@ -161,26 +186,30 @@ class LHEWeightsTableProducer : public edm::global::EDProducer("lheInfo", {"externalLHEProducer"}) ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)"); - //desc.add>("weightgroups"); desc.add("lheWeights"); desc.add("genWeights"); + desc.add>("weightgroups"); + desc.add>("maxGroupsPerType"); + desc.add>("pdfIds"); desc.add("lheWeightPrecision", -1)->setComment("Number of bits in the mantissa for LHE weights"); descriptions.addDefault(desc); } protected: - const edm::InputTag lheInputTag_; const edm::EDGetTokenT lheToken_; const edm::EDGetTokenT lheWeightToken_; const edm::EDGetTokenT lheWeightInfoToken_; const edm::EDGetTokenT genWeightToken_; const edm::EDGetTokenT genWeightInfoToken_; - //const std::vector weightgroups_; + const std::vector weightgroups_; + const std::vector maxGroupsPerType_; + const std::vector pdfIds_; const std::unordered_map weightTypeNames_ = { - {gen::kScaleWeights, "Scale"}, - {gen::kPdfWeights, "Pdf"}, - {gen::kMEParamWeights, "MEParam"}, - {gen::kUnknownWeights, "Unknown"}, + {gen::WeightType::kScaleWeights, "Scale"}, + {gen::WeightType::kPdfWeights, "Pdf"}, + {gen::WeightType::kMEParamWeights, "MEParam"}, + {gen::WeightType::kPartonShowerWeights, "PartonShower"}, + {gen::WeightType::kUnknownWeights, "Unknown"}, }; //std::unordered_map weightGroupIndices_; diff --git a/PhysicsTools/NanoAOD/python/nanogen_cff.py b/PhysicsTools/NanoAOD/python/nanogen_cff.py index 331880c6ba6c9..d2c155a48a5a6 100644 --- a/PhysicsTools/NanoAOD/python/nanogen_cff.py +++ b/PhysicsTools/NanoAOD/python/nanogen_cff.py @@ -5,7 +5,6 @@ from PhysicsTools.NanoAOD.particlelevel_cff import * from PhysicsTools.NanoAOD.lheInfoTable_cfi import * from PhysicsTools.NanoAOD.genWeightsTable_cfi import * -import ROOT genWeights = cms.EDProducer("GenWeightProductProducer") @@ -14,10 +13,12 @@ lheInfo = cms.InputTag("externalLHEProducer"), lheWeights = cms.InputTag("externalLHEProducer"), genWeights = cms.InputTag("genWeights"), - #weightgroups = cms.vint32([ROOT.gen.kScaleWeights, ROOT.gen.kMEParamWeights, ROOT.gen.kPdfWeights, ROOT.UnknownWeights]), - #weightgroups = cms.vint32([0,1,2,3,4]), - #numWeightgroups = cms.vint([1, -1, 1, 2, 1]), - #pdfs = cms.vint([91400, 306000, 260000]), + # Warning: you can use a full string, but only the first character is read. + # Note also that the capitalization is important! For example, 'parton shower' + # must be lower case and 'PDF' must be capital + weightgroups = cms.vstring(['scale', 'PDF', 'matrix element', 'unknown', 'shower']), + maxGroupsPerType = cms.vint32([1, -1, 1, 2, 1]), + pdfIds = cms.vint32([91400, 306000, 260000]), lheWeightPrecision = cms.int32(14), ) diff --git a/SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h index 9a1def0700e21..3b2dd09d37609 100644 --- a/SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h @@ -6,9 +6,9 @@ namespace gen { class MEParamWeightGroupInfo : public WeightGroupInfo { public: - MEParamWeightGroupInfo() : WeightGroupInfo() { weightType_ = kMEParamWeights; } + MEParamWeightGroupInfo() : WeightGroupInfo() { weightType_ = WeightType::kMEParamWeights; } MEParamWeightGroupInfo(std::string header, std::string name) : - WeightGroupInfo(header, name) { weightType_ = kMEParamWeights; } + WeightGroupInfo(header, name) { weightType_ = WeightType::kMEParamWeights; } MEParamWeightGroupInfo(std::string header) : MEParamWeightGroupInfo(header, header) {} virtual ~MEParamWeightGroupInfo() override {} diff --git a/SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h index e9bb539bcb185..ea6c1b1fba683 100644 --- a/SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/PartonShowerWeightGroupInfo.h @@ -9,7 +9,7 @@ namespace gen { PartonShowerWeightGroupInfo() : PartonShowerWeightGroupInfo("") {} PartonShowerWeightGroupInfo(std::string header, std::string name) : WeightGroupInfo(header, name) { - weightType_ = kPartonShowerWeights; + weightType_ = WeightType::kPartonShowerWeights; } PartonShowerWeightGroupInfo(std::string header) : PartonShowerWeightGroupInfo(header, header) { } diff --git a/SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h index 54556811ac4d8..aa69dc4bcb7b8 100644 --- a/SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h @@ -20,11 +20,11 @@ namespace gen { int alphasDownIndex_; std::vector> lhapdfIdsContained_; public: - PdfWeightGroupInfo() : WeightGroupInfo() { weightType_ = kPdfWeights; } + PdfWeightGroupInfo() : WeightGroupInfo() { weightType_ = WeightType::kPdfWeights; } PdfWeightGroupInfo(std::string header, std::string name) : - WeightGroupInfo(header, name) { weightType_ = kPdfWeights; } + WeightGroupInfo(header, name) { weightType_ = WeightType::kPdfWeights; } PdfWeightGroupInfo(std::string header) : - WeightGroupInfo(header) { weightType_ = kPdfWeights; } + WeightGroupInfo(header) { weightType_ = WeightType::kPdfWeights; } PdfWeightGroupInfo(const PdfWeightGroupInfo &other) { copy(other); } diff --git a/SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h index 994c1235963dd..d3feee93811be 100644 --- a/SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h @@ -20,7 +20,7 @@ namespace gen { ScaleWeightGroupInfo() : ScaleWeightGroupInfo("") {} ScaleWeightGroupInfo(std::string header, std::string name) : WeightGroupInfo(header, name) { - weightType_ = kScaleWeights; + weightType_ = WeightType::kScaleWeights; isFunctionalFormVar_ = false; centralIndex_ = 0; muR1muF2Index_ = 0; diff --git a/SimDataFormats/GeneratorProducts/interface/UnknownWeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/UnknownWeightGroupInfo.h index 37f68289633d7..66152d8840434 100644 --- a/SimDataFormats/GeneratorProducts/interface/UnknownWeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/UnknownWeightGroupInfo.h @@ -6,11 +6,11 @@ namespace gen { class UnknownWeightGroupInfo : public WeightGroupInfo { public: - UnknownWeightGroupInfo() : WeightGroupInfo() { weightType_ = kUnknownWeights; } + UnknownWeightGroupInfo() : WeightGroupInfo() { weightType_ = WeightType::kUnknownWeights; } UnknownWeightGroupInfo(std::string header, std::string name) : - WeightGroupInfo(header, name) { weightType_ = kUnknownWeights; isWellFormed_ = false;} + WeightGroupInfo(header, name) { weightType_ = WeightType::kUnknownWeights; isWellFormed_ = false;} UnknownWeightGroupInfo(std::string header) : - WeightGroupInfo(header) { weightType_ = kUnknownWeights; isWellFormed_ = false;} + WeightGroupInfo(header) { weightType_ = WeightType::kUnknownWeights; isWellFormed_ = false;} virtual ~UnknownWeightGroupInfo() override {} void copy(const UnknownWeightGroupInfo &other); virtual UnknownWeightGroupInfo* clone() const override; diff --git a/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h index e0fc9b2bdfb87..fc71436bd5ed6 100644 --- a/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace gen { struct WeightMetaInfo { @@ -16,14 +17,19 @@ namespace gen { std::string label; }; - enum WeightType { - kPdfWeights, - kScaleWeights, - kMEParamWeights, - kUnknownWeights, - kPartonShowerWeights, + enum class WeightType : char { + kPdfWeights = 'P', + kScaleWeights = 's', + kMEParamWeights = 'm', + kPartonShowerWeights = 'p', + kUnknownWeights = 'u', }; + const std::array allGenWeightTypes = {{WeightType::kPdfWeights, + WeightType::kScaleWeights, WeightType::kMEParamWeights, + WeightType::kPartonShowerWeights, WeightType::kUnknownWeights, + }}; + class WeightGroupInfo { public: WeightGroupInfo() : WeightGroupInfo("") {} diff --git a/SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc b/SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc index d6b42e3cc1f0e..2b08fce3ef09e 100644 --- a/SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc +++ b/SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc @@ -55,7 +55,7 @@ std::vector GenWeightInfoProduct::weightGroupsByType(gen: } std::optional GenWeightInfoProduct::pdfGroupWithIndexByLHAID(size_t lhaid) const { - auto pdfGroups = weightGroupsAndIndicesByType(gen::kPdfWeights); + auto pdfGroups = weightGroupsAndIndicesByType(gen::WeightType::kPdfWeights); auto matchingPdfSet = std::find_if(pdfGroups.begin(), pdfGroups.end(), [lhaid] (gen::WeightGroupData& data) {