Skip to content

Commit

Permalink
Fix bug in insert to containedIds for WeightGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed May 31, 2019
1 parent 49c25ba commit bb431b7
Show file tree
Hide file tree
Showing 7 changed files with 1,028 additions and 37 deletions.
952 changes: 952 additions & 0 deletions GeneratorInterface/LHEInterface/interface/TestWeightInfo.h

Large diffs are not rendered by default.

48 changes: 35 additions & 13 deletions GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ExternalLHEProducer : public edm::one::EDProducer<edm::BeginRunProducer,

int closeDescriptors(int preserve);
void executeScript();
int findWeightGroup(std::string id, int previousIndex);
int findWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex);
std::unique_ptr<std::string> readOutput();

void nextEvent();
Expand Down Expand Up @@ -212,11 +212,17 @@ ExternalLHEProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
int weightGroupIndex = 0;
int weightNum = 0;
for (const auto& weight : partonLevel->weights()) {
if (weightNum > 8)
continue;
weightGroupIndex = findWeightGroup(weight.id, weightGroupIndex);
std::cout << "Size is " << weightGroups_.size() << std::endl;
weightGroupIndex = findWeightGroup(weight.id, weightNum, weightGroupIndex);
std::cout << "Weight group index" << weightGroupIndex << std::endl;
std::cout << weightGroups_.at(1).name() << std::endl;
if (weightGroupIndex < 0) {
std::cout << "Yep that's the case.";
std::cout << " num Contained IDs " << weightGroups_.at(1).containedIds().size() << std::endl;
for (auto& id : weightGroups_.at(1).containedIds())
std::cout << id.id;
}
int entry = weightGroups_.at(weightGroupIndex).weightVectorEntry(weight.id, weightNum);
std::cout << "Matching entry is " << entry;
std::cout << "Still going. Entry is " << entry << std::endl;
weightProduct->addWeight(weight.wgt, weightGroupIndex, entry);
weightNum++;
Expand Down Expand Up @@ -353,15 +359,13 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es)
std::unique_ptr<LHEWeightInfoProduct> weightInfoProduct(new LHEWeightInfoProduct);
gen::WeightGroupInfo scaleInfo = getExampleScaleWeights();
//gen::WeightGroupInfo scaleInfo = getExampleScaleWeightsOutOfOrder();
std::vector<gen::WeightGroupInfo> pdfSets = getExamplePdfWeights();

gen::WeightGroupInfo cenPdfInfo(
"<weightgroup name=\"NNPDF31_nnlo_hessian_pdfas\" combine=\"hessian\">"
);
cenPdfInfo.setWeightType(gen::kPdfWeights);

weightInfoProduct->addWeightGroupInfo(scaleInfo);
weightInfoProduct->addWeightGroupInfo(cenPdfInfo);
for (auto& pdfSet : pdfSets)
weightInfoProduct->addWeightGroupInfo(pdfSet);
weightGroups_ = weightInfoProduct->allWeightGroupsInfo();
std::cout << "Number of groups is " << weightGroups_.size() << std::endl;
run.put(std::move(weightInfoProduct));

nextEvent();
Expand Down Expand Up @@ -537,8 +541,26 @@ ExternalLHEProducer::executeScript()
}


int ExternalLHEProducer::findWeightGroup(std::string wgtId, int previousIndex) {
return 0;
int ExternalLHEProducer::findWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex) {
// Start search at previous index, under expectation of ordered weights
std::cout << "Here we are";
for (int index = previousGroupIndex;
index < std::min(index+1, static_cast<int>(weightGroups_.size())); index++) {
auto& weightGroup = weightGroups_.at(previousGroupIndex);
// Fast search assuming order is not perturbed outside of weight group
if (weightGroup.indexInRange(weightIndex) && weightGroup.containsWeight(wgtId, weightIndex))
return static_cast<int>(index);
}
std::cout << "Done";

// Fall back to unordered search
int counter = 0;
for (auto& weightGroup : weightGroups_) {
if (weightGroup.containsWeight(wgtId, weightIndex))
return counter;
counter++;
}
return -1;
}

// ------------ Read the output script ------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class LHEWeightInfoProduct {

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:
Expand Down
10 changes: 3 additions & 7 deletions SimDataFormats/GeneratorProducts/interface/LHEWeightProduct.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ class LHEWeightProduct {
if (static_cast<int>(weightsVector_.size()) <= setEntry)
throw std::domain_error("Out of range weight");
auto& weights = weightsVector_.at(setEntry);
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);
if (static_cast<int>(weights.size()) <= weightNum) {
weights.resize(weightNum+1);
}
else
weights[weightNum] = weight;
weights[weightNum] = weight;
}
const WeightsContainer& weights() const { return weightsVector_; }

Expand Down
46 changes: 32 additions & 14 deletions SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace gen {
struct WeightMetaInfo {
int globalIndex;
int localIndex;
size_t globalIndex;
size_t localIndex;
std::string id;
std::string label;
};
Expand All @@ -29,7 +29,7 @@ namespace gen {
WeightGroupInfo(std::string header, std::string name):
headerEntry_(header), name_(name), firstId_(-1), lastId_(-1) {}
WeightGroupInfo(std::string header):
headerEntry_(header), name_(header), firstId_(0), lastId_(0) {}
headerEntry_(header), name_(header), firstId_(-1), lastId_(-1) {}

WeightMetaInfo weightMetaInfo(int weightEntry) {
return idsContained_.at(weightEntry);
Expand All @@ -44,12 +44,18 @@ namespace gen {
return weightVectorEntry(wgtId, 0);
}

int containsWeight(const std::string& wgtId, int weightEntry) {
return weightVectorEntry(wgtId, weightEntry) != -1;
}

int weightVectorEntry(const std::string& wgtId, int weightEntry) {
int orderedEntry = weightEntry - firstId_;
int entry = -1;
if (orderedEntry >= 0 && static_cast<size_t>(orderedEntry) < idsContained_.size())
if (idsContained_.at(orderedEntry).id == wgtId)
return orderedEntry;
if (!indexInRange(weightEntry)) {
size_t orderedEntry = weightEntry - firstId_;
if (orderedEntry < idsContained_.size())
if (idsContained_.at(orderedEntry).id == wgtId)
return orderedEntry;
}
auto it = std::find_if(idsContained_.begin(), idsContained_.end(),
[wgtId] (const WeightMetaInfo& w) { return w.id == wgtId; });
if (it != idsContained_.end())
Expand All @@ -58,26 +64,38 @@ namespace gen {
}

void addContainedId(int weightEntry, std::string id, std::string label="") {
if (firstId_ == -1 || weightEntry < firstId_)
if (firstId_ == -1 || weightEntry < firstId_) {
firstId_ = weightEntry;
if (firstId_ == -1 || weightEntry > lastId_)
// Reset to reflect that indices will be shifted
for (auto& id : idsContained_)
id.localIndex = id.globalIndex - firstId_;
}
if (weightEntry > lastId_)
lastId_ = weightEntry;

int orderedEntry = weightEntry - firstId_;
WeightMetaInfo info;
info.globalIndex = weightEntry;
info.localIndex = orderedEntry;
info.localIndex = weightEntry - firstId_;
info.id = id;
info.label = label;

if (static_cast<int>(idsContained_.size()) < orderedEntry)
idsContained_.resize(orderedEntry);
idsContained_.insert(idsContained_.begin()+orderedEntry, info);
if (idsContained_.size() < info.localIndex) {
idsContained_.resize(info.localIndex);
idsContained_.insert(idsContained_.begin()+info.localIndex, info);
}
else if (idsContained_.size() == info.localIndex) {
idsContained_.push_back(info);
}
else {
idsContained_.resize(info.localIndex+1);
idsContained_[info.localIndex] = info;
}
}

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

void setWeightType(WeightType type) { weightType_ = type; }
std::string name() { return name_; }

bool indexInRange(int index) const {
return (index <= lastId_ && index >= firstId_);
Expand Down
4 changes: 4 additions & 0 deletions SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const gen::WeightGroupInfo& LHEWeightInfoProduct::containingWeightGroupInfo(int
throw std::domain_error("Failed to find containing weight group");
}

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

void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo info) {
weightGroupsInfo_.push_back(info);
}
4 changes: 1 addition & 3 deletions SimDataFormats/GeneratorProducts/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@
<class name="gen::WeightGroupInfo" ClassVersion="6">
<version ClassVersion="6" checksum="1055618343"/>
</class>
<class name="gen::WeightMetaInfo" ClassVersion="4">
<version ClassVersion="4" checksum="3369458440"/>
</class>
<class name="gen::WeightMetaInfo"/>
<class name="std::vector<gen::WeightsInfo>"/>
<class name="std::vector<gen::WeightGroupInfo>"/>
<class name="edm::Wrapper&lt;LHERunInfoProduct&gt;"/>
Expand Down

0 comments on commit bb431b7

Please sign in to comment.