Skip to content

Commit

Permalink
Example with both scale and ME weights
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Jan 5, 2020
1 parent fb19c05 commit ae40865
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
41 changes: 29 additions & 12 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
#include <tinyxml2.h>

namespace {
struct WeightGroupData {
size_t index;
const gen::WeightGroupInfo* group;
};

typedef std::unordered_map<std::string, const WeightGroupData> WeightGroupsToStore;
typedef std::unordered_map<std::string, gen::WeightGroupData> WeightGroupsToStore;
} // namespace

class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBlockCache<WeightGroupsToStore>> {
Expand Down Expand Up @@ -60,6 +55,12 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
lheWeightTables->back().addColumn<float>(
"", scaleWeights, weightInfos.at("Scale").group->name(), nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);

auto meWeights = meReweightWeights(lheWeights, weightInfos.at("MEReweight"), w0);
lheWeightTables->emplace_back(meWeights.size(), "LHEMEReweightWeight", false);
lheWeightTables->back().addColumn<float>(
"", meWeights, weightInfos.at("MEReweight").group->name(), nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);


iEvent.put(std::move(lheWeightTables));
}

Expand All @@ -71,16 +72,32 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
edm::Handle<GenWeightInfoProduct> lheWeightInfoHandle;
iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle);

// Should add a search by name function
auto scaleGroupIndices = lheWeightInfoHandle->weightGroupIndicesByType(gen::kScaleWeights);
size_t scaleGroupIndex = scaleGroupIndices.front();
const gen::WeightGroupInfo* scaleGroupInfo = lheWeightInfoHandle->orderedWeightGroupInfo(scaleGroupIndex);
WeightGroupsToStore weightsToStore = { {"Scale", {scaleGroupIndex, scaleGroupInfo}} };
std::vector<gen::WeightGroupData> scaleGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kScaleWeights);
auto meGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kMEParamWeights);

WeightGroupsToStore weightsToStore;
weightsToStore.insert({"Scale", scaleGroups.front()});
weightsToStore.insert({"MEReweight", meGroups.front()});
// {"MEReweight", meGroups.at(0)},
//};
//i = 0;
//for (const auto& meGroup : meGroups) {
// std::string label = "MEReweight";
// label = scaleGroups.size() > 1 ? label + i : label;
// weightsToStore[label] = meGroup;
//}

return std::make_shared<WeightGroupsToStore>(weightsToStore);
}

std::vector<float> orderedScaleWeights(WeightsContainer& lheWeights, const WeightGroupData& scaleGroupInfo, double w0) const {
std::vector<float> meReweightWeights(WeightsContainer& lheWeights, const gen::WeightGroupData& meGroupInfo, double w0) const {
std::vector<float> normalizedWeights;
for (const auto& weight : lheWeights.at(meGroupInfo.index))
normalizedWeights.push_back(weight/w0);
return normalizedWeights;
}

std::vector<float> orderedScaleWeights(WeightsContainer& lheWeights, const gen::WeightGroupData& scaleGroupInfo, double w0) const {
const gen::ScaleWeightGroupInfo* scaleGroup = static_cast<const gen::ScaleWeightGroupInfo*>(scaleGroupInfo.group);
size_t scaleGroupIndex = scaleGroupInfo.index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
#include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h"

namespace gen {
struct WeightGroupData {
size_t index;
const gen::WeightGroupInfo* group;
};
} // namespace

class GenWeightInfoProduct {
public:
GenWeightInfoProduct() {}
Expand All @@ -27,6 +34,7 @@ class GenWeightInfoProduct {
const gen::WeightGroupInfo* orderedWeightGroupInfo(int index) const;
std::vector<gen::WeightGroupInfo*> weightGroupsByType(gen::WeightType type) const;
std::vector<int> weightGroupIndicesByType(gen::WeightType type) const;
std::vector<gen::WeightGroupData> weightGroupsAndIndicesByType(gen::WeightType type) const;
void addWeightGroupInfo(gen::WeightGroupInfo* info);
const int numberOfGroups() const { return weightGroupsInfo_.size(); }

Expand Down
9 changes: 9 additions & 0 deletions SimDataFormats/GeneratorProducts/src/GenWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ const gen::WeightGroupInfo* GenWeightInfoProduct::orderedWeightGroupInfo(int wei
return &weightGroupsInfo_[weightGroupIndex];
}

std::vector<gen::WeightGroupData> GenWeightInfoProduct::weightGroupsAndIndicesByType(gen::WeightType type) const {
std::vector<gen::WeightGroupData> matchingGroups;
for (size_t i = 0; i < weightGroupsInfo_.size(); i++) {
if (weightGroupsInfo_[i].weightType() == type)
matchingGroups.push_back({i, weightGroupsInfo_[i].clone()});
}
return matchingGroups;
}

std::vector<gen::WeightGroupInfo*> GenWeightInfoProduct::weightGroupsByType(gen::WeightType type) const {
std::vector<gen::WeightGroupInfo*> matchingGroups;
for (size_t i = 0; i < weightGroupsInfo_.size(); i++) {
Expand Down

0 comments on commit ae40865

Please sign in to comment.