From 49c25ba7a201cac7dc3c516f0dd8be95e4901509 Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Thu, 30 May 2019 16:14:26 -0500 Subject: [PATCH] Working rough implementation of Weight/WeightInfo prods --- .../LHEInterface/interface/TestWeightInfo.h | 34 ++++++++++++++++++- .../plugins/ExternalLHEProducer.cc | 3 +- .../interface/LHEWeightInfoProduct.h | 3 +- .../interface/LHEWeightProduct.h | 3 +- .../interface/WeightGroupInfo.h | 21 ++++++++++-- .../src/LHEWeightInfoProduct.cc | 10 +++++- 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h b/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h index 451c60c03eed9..9ae3212728f77 100644 --- a/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h +++ b/GeneratorInterface/LHEInterface/interface/TestWeightInfo.h @@ -1,4 +1,12 @@ #include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h" +#include + +std::string parseId(std::string label) { + auto re = std::regex("id=\"([0-9]*)\""); + std::smatch matches; + std::regex_search(label, matches, re); + return std::string(matches.str(1)); +} gen::WeightGroupInfo getExampleScaleWeights() { gen::WeightGroupInfo scaleInfo( @@ -19,7 +27,31 @@ gen::WeightGroupInfo getExampleScaleWeights() { scaleInfo.setWeightType(gen::kScaleWeights); for (size_t i = 0; i < entries.size(); i++) { - scaleInfo.addContainedId(i, std::to_string(i+1), entries[i]); + scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]); + } + return scaleInfo; +} + +gen::WeightGroupInfo getExampleScaleWeightsOutOfOrder() { + gen::WeightGroupInfo scaleInfo( + "", + "centralScaleVariations" + ); + std::vector entries = { + R"( mur=1 muf=1 )", + R"( mur=1 muf=2 )", + R"( mur=1 muf=0.5 )", + R"( mur=2 muf=2 )", + R"( mur=2 muf=1 )", + R"( mur=2 muf=0.5 )", + R"( mur=0.5 muf=2 )", + R"( mur=0.5 muf=1 )", + R"( mur=0.5 muf=0.5 )", + }; + scaleInfo.setWeightType(gen::kScaleWeights); + + for (size_t i = 0; i < entries.size(); i++) { + scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]); } return scaleInfo; } diff --git a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc index ee3a7ea3e6d8d..763437e587a98 100644 --- a/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc +++ b/GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc @@ -352,6 +352,7 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es) std::unique_ptr weightInfoProduct(new LHEWeightInfoProduct); gen::WeightGroupInfo scaleInfo = getExampleScaleWeights(); + //gen::WeightGroupInfo scaleInfo = getExampleScaleWeightsOutOfOrder(); gen::WeightGroupInfo cenPdfInfo( "" @@ -360,7 +361,7 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es) weightInfoProduct->addWeightGroupInfo(scaleInfo); weightInfoProduct->addWeightGroupInfo(cenPdfInfo); - weightGroups_ = weightInfoProduct->getWeightGroupsInfo(); + weightGroups_ = weightInfoProduct->allWeightGroupsInfo(); run.put(std::move(weightInfoProduct)); nextEvent(); diff --git a/SimDataFormats/GeneratorProducts/interface/LHEWeightInfoProduct.h b/SimDataFormats/GeneratorProducts/interface/LHEWeightInfoProduct.h index 6ab385ae70cdf..a68418dabecdb 100644 --- a/SimDataFormats/GeneratorProducts/interface/LHEWeightInfoProduct.h +++ b/SimDataFormats/GeneratorProducts/interface/LHEWeightInfoProduct.h @@ -21,7 +21,8 @@ class LHEWeightInfoProduct { LHEWeightInfoProduct& operator=(const LHEWeightInfoProduct &other); LHEWeightInfoProduct& operator=(LHEWeightInfoProduct &&other); - std::vector getWeightGroupsInfo(); + const std::vector& allWeightGroupsInfo() const; + const gen::WeightGroupInfo& containingWeightGroupInfo(int index) const; void addWeightGroupInfo(gen::WeightGroupInfo info); private: diff --git a/SimDataFormats/GeneratorProducts/interface/LHEWeightProduct.h b/SimDataFormats/GeneratorProducts/interface/LHEWeightProduct.h index e58927987bdd3..0e8ecc1ed2408 100644 --- a/SimDataFormats/GeneratorProducts/interface/LHEWeightProduct.h +++ b/SimDataFormats/GeneratorProducts/interface/LHEWeightProduct.h @@ -28,7 +28,6 @@ class LHEWeightProduct { if (static_cast(weightsVector_.size()) <= setEntry) throw std::domain_error("Out of range weight"); auto& weights = weightsVector_.at(setEntry); - std::cout << "Weights size is " << weights.size() << std::endl; if (static_cast(weights.size()) == weightNum) weights.push_back(weight); else if (static_cast(weights.size()) < weightNum) { @@ -36,7 +35,7 @@ class LHEWeightProduct { weights.insert(weights.begin()+weightNum, weight); } else - weights.insert(weights.begin()+weightNum, weight); + weights[weightNum] = weight; } const WeightsContainer& weights() const { return weightsVector_; } diff --git a/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h b/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h index 43e189df5c50b..75a2dccbf7f8e 100644 --- a/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h +++ b/SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h @@ -5,6 +5,7 @@ * */ #include +#include namespace gen { struct WeightMetaInfo { @@ -30,6 +31,15 @@ namespace gen { WeightGroupInfo(std::string header): headerEntry_(header), name_(header), firstId_(0), lastId_(0) {} + WeightMetaInfo weightMetaInfo(int weightEntry) { + return idsContained_.at(weightEntry); + } + + WeightMetaInfo weightMetaInfo(std::string wgtId) { + int weightEntry = weightVectorEntry(wgtId); + return idsContained_.at(weightEntry); + } + int weightVectorEntry(const std::string& wgtId) { return weightVectorEntry(wgtId, 0); } @@ -40,7 +50,10 @@ namespace gen { if (orderedEntry >= 0 && static_cast(orderedEntry) < idsContained_.size()) if (idsContained_.at(orderedEntry).id == wgtId) return orderedEntry; - //auto it = std::find( + auto it = std::find_if(idsContained_.begin(), idsContained_.end(), + [wgtId] (const WeightMetaInfo& w) { return w.id == wgtId; }); + if (it != idsContained_.end()) + return std::distance(idsContained_.begin(), it); return entry; } @@ -57,14 +70,16 @@ namespace gen { info.id = id; info.label = label; + if (static_cast(idsContained_.size()) < orderedEntry) + idsContained_.resize(orderedEntry); idsContained_.insert(idsContained_.begin()+orderedEntry, info); } - std::vector containedIds() { return idsContained_; } + std::vector containedIds() const { return idsContained_; } void setWeightType(WeightType type) { weightType_ = type; } - bool indexInRange(int index) { + bool indexInRange(int index) const { return (index <= lastId_ && index >= firstId_); } diff --git a/SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc b/SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc index 7aee2dbff205a..d20b76b934189 100644 --- a/SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc +++ b/SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc @@ -17,10 +17,18 @@ LHEWeightInfoProduct& LHEWeightInfoProduct::operator=(LHEWeightInfoProduct &&oth return *this; } -std::vector LHEWeightInfoProduct::getWeightGroupsInfo() { +const std::vector& LHEWeightInfoProduct::allWeightGroupsInfo() const { return weightGroupsInfo_; } +const gen::WeightGroupInfo& LHEWeightInfoProduct::containingWeightGroupInfo(int index) const { + for (const auto& weightGroup : weightGroupsInfo_) { + if (weightGroup.indexInRange(index)) + return weightGroup; + } + throw std::domain_error("Failed to find containing weight group"); +} + void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo info) { weightGroupsInfo_.push_back(info); }