Skip to content

Commit

Permalink
Create entry for unassociated weights if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Oct 5, 2020
1 parent f46cc33 commit 41c79e2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace gen {
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<gen::WeightsInfo>, float w0);
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<double>, float w0);
void setModel(std::string model) { model_ = model; }
void addUnassociatedGroup() { weightGroups_.push_back(new UnknownWeightGroupInfo("unassociated")); }
int addWeightToProduct(
std::unique_ptr<GenWeightProduct>& product, double weight, std::string name, int weightNum, int groupIndex);
int findContainingWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex);
Expand Down
3 changes: 3 additions & 0 deletions GeneratorInterface/Core/plugins/GenWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ void GenWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock&
weightHelper_.parseWeightGroupsFromNames(weightNames_);

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
if (weightHelper_.weightGroups().size() == 0)
weightHelper_.addUnassociatedGroup();

for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
}
Expand Down
28 changes: 25 additions & 3 deletions GeneratorInterface/Core/src/WeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,31 @@ namespace gen {

int WeightHelper::addWeightToProduct(
std::unique_ptr<GenWeightProduct>& product, double weight, std::string name, int weightNum, int groupIndex) {
groupIndex = findContainingWeightGroup(name, weightNum, groupIndex);
auto group = weightGroups_[groupIndex];
int entry = group.weightVectorEntry(name, weightNum);
bool isUnassociated = false;
try {
groupIndex = findContainingWeightGroup(name, weightNum, groupIndex);
}
catch (const std::range_error& e) {
std::cerr << "WARNING: " << e.what() << std::endl;
isUnassociated = true;

bool foundUnassocGroup = false;
while (!foundUnassocGroup && groupIndex < static_cast<int>(weightGroups_.size())) {
auto& g = weightGroups_[groupIndex];
if (g.weightType() == gen::WeightType::kUnknownWeights && g.name() == "unassociated")
foundUnassocGroup = true;
else
groupIndex++;
}
if (!foundUnassocGroup) {
addUnassociatedGroup();
}
}
auto& group = weightGroups_[groupIndex];
if (isUnassociated) {
group.addContainedId(weightNum, name, name);
}
int entry = !isUnassociated ? group.weightVectorEntry(name, weightNum) : group.nIdsContained();
if (debug_)
std::cout << "Adding weight " << entry << " to group " << groupIndex << std::endl;
product->addWeight(weight, groupIndex, entry);
Expand Down
1 change: 0 additions & 1 deletion PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ void LHEWeightsTableProducer::produce(edm::StreamID id, edm::Event& iEvent, cons
std::string wname = wg.second;
auto& weightVec = lheWeightTables[wg.first];
counter.incLHE(genWeight, weightVec, wname);
std::cout << "Writing out weight of type:" << wname << std::endl;
auto outTable = std::make_unique<nanoaod::FlatTable>(weightVec.size(), wname + "Weight", false);
outTable->addColumn<float>("", weightVec, weightlabels[wg.first],
nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
Expand Down

0 comments on commit 41c79e2

Please sign in to comment.