Skip to content

Commit

Permalink
Store pointers to WeightGroupInfo
Browse files Browse the repository at this point in the history
First step to moving towards edm::OwnVector and polymorphic storage
  • Loading branch information
kdlong committed Aug 5, 2019
1 parent df84164 commit 23bee66
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 44 deletions.
30 changes: 13 additions & 17 deletions GeneratorInterface/LHEInterface/interface/TestWeightInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ std::string parseId(std::string label) {
return std::string(matches.str(1));
}

gen::WeightGroupInfo getExampleScaleWeights() {
gen::WeightGroupInfo scaleInfo(
gen::WeightGroupInfo* getExampleScaleWeights() {
gen::WeightGroupInfo* scaleInfo = new gen::WeightGroupInfo(
"<weightgroup combine=\"envelope\" name=\"Central scale variation\">",
"centralScaleVariations"
);
Expand All @@ -24,16 +24,16 @@ gen::WeightGroupInfo getExampleScaleWeights() {
R"(<weight MUF="2" MUR="0.5" PDF="263400" id="8"> mur=0.5 muf=2 </weight>)",
R"(<weight MUF="0.5" MUR="0.5" PDF="263400" id="9"> mur=0.5 muf=0.5 </weight>)",
};
scaleInfo.setWeightType(gen::kScaleWeights);
scaleInfo->setWeightType(gen::kScaleWeights);

for (size_t i = 0; i < entries.size(); i++) {
scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]);
scaleInfo->addContainedId(i, parseId(entries[i]), entries[i]);
}
return scaleInfo;
}

gen::WeightGroupInfo getExampleScaleWeightsOutOfOrder() {
gen::WeightGroupInfo scaleInfo(
gen::WeightGroupInfo* getExampleScaleWeightsOutOfOrder() {
gen::WeightGroupInfo* scaleInfo = new gen::WeightGroupInfo(
"<weightgroup combine=\"envelope\" name=\"Central scale variation\">",
"centralScaleVariations"
);
Expand All @@ -48,19 +48,15 @@ gen::WeightGroupInfo getExampleScaleWeightsOutOfOrder() {
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);
scaleInfo->setWeightType(gen::kScaleWeights);

for (size_t i = 0; i < entries.size(); i++) {
scaleInfo.addContainedId(i, parseId(entries[i]), entries[i]);
scaleInfo->addContainedId(i, parseId(entries[i]), entries[i]);
}
return scaleInfo;
}

std::vector<gen::WeightGroupInfo> getExamplePdfWeights() {
gen::WeightGroupInfo scaleInfo(
"<weightgroup combine=\"envelope\" name=\"Central scale variation\">",
"centralScaleVariations"
);
std::vector<gen::WeightGroupInfo*> getExamplePdfWeights() {
std::vector<std::string> entries = {
R"(<weightgroup combine="hessian" name="NNPDF31_nnlo_hessian_pdfas">)",
R"(<weight MUF="1" MUR="1" PDF="306000" id="10"> Member 0 of sets NNPDF31_nnlo_hessian_pdfas</weight>)",
Expand Down Expand Up @@ -991,18 +987,18 @@ std::vector<gen::WeightGroupInfo> getExamplePdfWeights() {
R"(</weightgroup>)",
};

std::vector<gen::WeightGroupInfo> pdfWeights;
std::vector<gen::WeightGroupInfo*> pdfWeights;

//Don't forget about the scale weights
int counter = 8;
for (const auto& entry : entries) {
if (entry.find("<weightgroup") != std::string::npos) {
pdfWeights.push_back(gen::WeightGroupInfo(entry));
pdfWeights.push_back(new gen::WeightGroupInfo(entry));
}
else if (entry.find("</weightgroup") == std::string::npos) {
std::cout << "Adding! " << entry << " ID " << parseId(entry) << std::endl;
auto& currentSet = pdfWeights.back();
currentSet.addContainedId(counter, parseId(entry), entry);
auto currentSet = pdfWeights.back();
currentSet->addContainedId(counter, parseId(entry), entry);
}
counter++;
}
Expand Down
21 changes: 12 additions & 9 deletions GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ExternalLHEProducer : public edm::one::EDProducer<edm::BeginRunProducer,
std::shared_ptr<lhef::LHEEvent> partonLevel;
boost::ptr_deque<LHERunInfoProduct> runInfoProducts;
bool wasMerged;
std::vector<gen::WeightGroupInfo> weightGroups_;
std::vector<gen::WeightGroupInfo*> weightGroups_;

class FileCloseSentry : private boost::noncopyable {
public:
Expand Down Expand Up @@ -213,7 +213,10 @@ ExternalLHEProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
int weightNum = 0;
for (const auto& weight : partonLevel->weights()) {
weightGroupIndex = findWeightGroup(weight.id, weightNum, weightGroupIndex);
int entry = weightGroups_.at(weightGroupIndex).weightVectorEntry(weight.id, weightNum);
auto group = weightGroups_.at(weightGroupIndex);
if (!group)
throw std::out_of_range("Invalid index " + weightGroupIndex);
int entry = group->weightVectorEntry(weight.id, weightNum);
weightProduct->addWeight(weight.wgt, weightGroupIndex, entry);
weightNum++;
}
Expand Down Expand Up @@ -347,12 +350,12 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es)
reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);

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

weightInfoProduct->addWeightGroupInfo(scaleInfo);
for (auto& pdfSet : pdfSets)
for (auto pdfSet : pdfSets)
weightInfoProduct->addWeightGroupInfo(pdfSet);
weightGroups_ = weightInfoProduct->allWeightGroupsInfo();
run.put(std::move(weightInfoProduct));
Expand Down Expand Up @@ -534,16 +537,16 @@ int ExternalLHEProducer::findWeightGroup(std::string wgtId, int weightIndex, int
// Start search at previous index, under expectation of ordered weights
for (int index = previousGroupIndex;
index < std::min(index+1, static_cast<int>(weightGroups_.size())); index++) {
auto& weightGroup = weightGroups_.at(previousGroupIndex);
gen::WeightGroupInfo* weightGroup = weightGroups_.at(previousGroupIndex);
// Fast search assuming order is not perturbed outside of weight group
if (weightGroup.indexInRange(weightIndex) && weightGroup.containsWeight(wgtId, weightIndex))
if (weightGroup->indexInRange(weightIndex) && weightGroup->containsWeight(wgtId, weightIndex))
return static_cast<int>(index);
}

// Fall back to unordered search
int counter = 0;
for (auto& weightGroup : weightGroups_) {
if (weightGroup.containsWeight(wgtId, weightIndex))
for (auto weightGroup : weightGroups_) {
if (weightGroup->containsWeight(wgtId, weightIndex))
return counter;
counter++;
}
Expand Down
4 changes: 2 additions & 2 deletions GeneratorInterface/LHEInterface/plugins/LHESource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void LHESource::putWeightInfoProduct(edm::RunPrincipal& iRunPrincipal) {
);
cenPdfInfo.setWeightType(gen::kPdfWeights);

product->addWeightGroupInfo(scaleInfo);
product->addWeightGroupInfo(cenPdfInfo);
product->addWeightGroupInfo(&scaleInfo);
product->addWeightGroupInfo(&cenPdfInfo);
std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHEWeightInfoProduct>(std::move(product)));
iRunPrincipal.put(lheProvenanceHelper_.weightProductBranchDescription_, std::move(rdp));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
class LHEWeightInfoProduct {
public:
LHEWeightInfoProduct() {}
LHEWeightInfoProduct(std::vector<gen::WeightGroupInfo>& weightGroups);
LHEWeightInfoProduct(std::vector<gen::WeightGroupInfo*>& weightGroups);
LHEWeightInfoProduct(const LHEWeightInfoProduct& other);
LHEWeightInfoProduct(LHEWeightInfoProduct&& other);
~LHEWeightInfoProduct() {}
LHEWeightInfoProduct& operator=(const LHEWeightInfoProduct &other);
LHEWeightInfoProduct& operator=(LHEWeightInfoProduct &&other);

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

private:
std::vector<gen::WeightGroupInfo> weightGroupsInfo_;
std::vector<gen::WeightGroupInfo*> weightGroupsInfo_;


};
Expand Down
14 changes: 7 additions & 7 deletions SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "SimDataFormats/GeneratorProducts/interface/LHEWeightInfoProduct.h"

LHEWeightInfoProduct::LHEWeightInfoProduct(std::vector<gen::WeightGroupInfo>& weightGroups) {
LHEWeightInfoProduct::LHEWeightInfoProduct(std::vector<gen::WeightGroupInfo*>& weightGroups) {
weightGroupsInfo_ = weightGroups;
}

Expand All @@ -17,22 +17,22 @@ LHEWeightInfoProduct& LHEWeightInfoProduct::operator=(LHEWeightInfoProduct &&oth
return *this;
}

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

const gen::WeightGroupInfo& LHEWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
const gen::WeightGroupInfo* LHEWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
return weightGroupsInfo_.at(weightGroupIndex);
}

void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo info) {
void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo* info) {
weightGroupsInfo_.push_back(info);
}
6 changes: 3 additions & 3 deletions SimDataFormats/GeneratorProducts/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@
<class name="LHERunInfoProduct::Header" ClassVersion="10">
<version ClassVersion="10" checksum="1310911082"/>
</class>
<class name="LHEWeightInfoProduct" ClassVersion="4">
<version ClassVersion="4" checksum="829980141"/>
</class>
<class name="LHEWeightInfoProduct"/>
<class name="LHEWeightProduct"/>
<class name="LHEXMLStringProduct" ClassVersion="11">
<version ClassVersion="10" checksum="4158740350"/>
Expand Down Expand Up @@ -231,6 +229,8 @@
<class name="gen::WeightMetaInfo"/>
<class name="std::vector<gen::WeightsInfo>"/>
<class name="std::vector<gen::WeightGroupInfo>"/>
<class name="std::vector<gen::WeightGroupInfo*>"/>
<class name="edm::OwnVector<gen::WeightGroupInfo>"/>
<class name="edm::Wrapper&lt;LHERunInfoProduct&gt;"/>
<class name="edm::Wrapper&lt;LHEWeightInfoProduct&gt;"/>
<class name="edm::Wrapper&lt;LHEWeightProduct&gt;"/>
Expand Down

0 comments on commit 23bee66

Please sign in to comment.