Skip to content

Commit

Permalink
Working rough implementation of Weight/WeightInfo prods
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed May 30, 2019
1 parent 444dbc3 commit 49c25ba
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
34 changes: 33 additions & 1 deletion GeneratorInterface/LHEInterface/interface/TestWeightInfo.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h"
#include <regex>

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(
Expand All @@ -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(
"<weightgroup combine=\"envelope\" name=\"Central scale variation\">",
"centralScaleVariations"
);
std::vector<std::string> entries = {
R"(<weight MUF="1" MUR="1" PDF="263400" id="1"> mur=1 muf=1 </weight>)",
R"(<weight MUF="2" MUR="1" PDF="263400" id="2"> mur=1 muf=2 </weight>)",
R"(<weight MUF="0.5" MUR="1" PDF="263400" id="3"> mur=1 muf=0.5 </weight>)",
R"(<weight MUF="2" MUR="2" PDF="263400" id="5"> mur=2 muf=2 </weight>)",
R"(<weight MUF="1" MUR="2" PDF="263400" id="4"> mur=2 muf=1 </weight>)",
R"(<weight MUF="0.5" MUR="2" PDF="263400" id="6"> mur=2 muf=0.5 </weight>)",
R"(<weight MUF="2" MUR="0.5" PDF="263400" id="8"> mur=0.5 muf=2 </weight>)",
R"(<weight MUF="1" MUR="0.5" PDF="263400" id="7"> mur=0.5 muf=1 </weight>)",
R"(<weight MUF="0.5" MUR="0.5" PDF="263400" id="9"> mur=0.5 muf=0.5 </weight>)",
};
scaleInfo.setWeightType(gen::kScaleWeights);

for (size_t i = 0; i < entries.size(); i++) {
scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]);
}
return scaleInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es)

std::unique_ptr<LHEWeightInfoProduct> weightInfoProduct(new LHEWeightInfoProduct);
gen::WeightGroupInfo scaleInfo = getExampleScaleWeights();
//gen::WeightGroupInfo scaleInfo = getExampleScaleWeightsOutOfOrder();

gen::WeightGroupInfo cenPdfInfo(
"<weightgroup name=\"NNPDF31_nnlo_hessian_pdfas\" combine=\"hessian\">"
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class LHEWeightInfoProduct {
LHEWeightInfoProduct& operator=(const LHEWeightInfoProduct &other);
LHEWeightInfoProduct& operator=(LHEWeightInfoProduct &&other);

std::vector<gen::WeightGroupInfo> getWeightGroupsInfo();
const std::vector<gen::WeightGroupInfo>& allWeightGroupsInfo() const;
const gen::WeightGroupInfo& containingWeightGroupInfo(int index) const;
void addWeightGroupInfo(gen::WeightGroupInfo info);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ class LHEWeightProduct {
if (static_cast<int>(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<int>(weights.size()) == weightNum)
weights.push_back(weight);
else if (static_cast<int>(weights.size()) < weightNum) {
weights.resize(weightNum);
weights.insert(weights.begin()+weightNum, weight);
}
else
weights.insert(weights.begin()+weightNum, weight);
weights[weightNum] = weight;
}
const WeightsContainer& weights() const { return weightsVector_; }

Expand Down
21 changes: 18 additions & 3 deletions SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/
#include <string>
#include <algorithm>

namespace gen {
struct WeightMetaInfo {
Expand All @@ -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);
}
Expand All @@ -40,7 +50,10 @@ namespace gen {
if (orderedEntry >= 0 && static_cast<size_t>(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;
}

Expand All @@ -57,14 +70,16 @@ namespace gen {
info.id = id;
info.label = label;

if (static_cast<int>(idsContained_.size()) < orderedEntry)
idsContained_.resize(orderedEntry);
idsContained_.insert(idsContained_.begin()+orderedEntry, info);
}

std::vector<WeightMetaInfo> containedIds() { return idsContained_; }
std::vector<WeightMetaInfo> containedIds() const { return idsContained_; }

void setWeightType(WeightType type) { weightType_ = type; }

bool indexInRange(int index) {
bool indexInRange(int index) const {
return (index <= lastId_ && index >= firstId_);
}

Expand Down
10 changes: 9 additions & 1 deletion SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ LHEWeightInfoProduct& LHEWeightInfoProduct::operator=(LHEWeightInfoProduct &&oth
return *this;
}

std::vector<gen::WeightGroupInfo> LHEWeightInfoProduct::getWeightGroupsInfo() {
const std::vector<gen::WeightGroupInfo>& 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);
}

0 comments on commit 49c25ba

Please sign in to comment.