Skip to content

Commit

Permalink
select specific weight groups
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Dec 19, 2019
1 parent 205de35 commit d663d2f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::RunCache<std
LHEWeightsTableProducer(edm::ParameterSet const& params)
: lheInputTag_(params.getParameter<edm::InputTag>("lheInfo")),
lheToken_(consumes<LHEEventProduct>(params.getParameter<edm::InputTag>("lheInfo"))),
weightgroups_(params.getParameter<std::vector<std::string>>("weightgroups")),
lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")) {
consumes<LHERunInfoProduct, edm::InRun>(lheInputTag_);
produces<std::vector<nanoaod::FlatTable>>();
Expand All @@ -90,20 +91,26 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::RunCache<std
if (lheInfo.weights().size() != weightInfos.size()) {
throw cms::Exception("LogicError", "Weight labels don't have the same size as weights!\n");
}
std::unordered_map<std::string, std::vector<float>> groupsWithWeights;
std::unordered_map<std::string, std::pair<std::string, std::vector<float>>> groupsWithWeights;
for (auto const& weight : lheInfo.weights()) {
if (!weightInfos[i].group) {
groupsWithWeights["ungrouped"].push_back(weight.wgt / w0);
auto& val = weightInfos[i].group ? groupsWithWeights[*weightInfos[i].group] : groupsWithWeights["ungrouped"];
if(val.first.empty()) {
val.first += ";id,text";
}
groupsWithWeights[*weightInfos[i].group].push_back(weight.wgt / w0);
val.first += ";" + weightInfos[i].id + "," + weightInfos[i].text;
val.second.push_back(weight.wgt / w0);
++i;
}
for (auto const& group : groupsWithWeights) {
if(std::find(weightgroups_.begin(), weightgroups_.end(), group.first) == weightgroups_.end()) {
continue;
}
std::string name = std::string("LHEWeight_") + group.first;
std::transform(name.begin(), name.end(), name.begin(), [](char ch) { return ch == ' ' ? '_' : ch; });
lheWeightTables->emplace_back(group.second.size(), name, false);
std::string doc = group.first + " (w_var / w_nominal)" + group.second.first;
lheWeightTables->emplace_back(group.second.second.size(), name, false);
lheWeightTables->back().addColumn<float>(
"", group.second, group.first + " (w_var / w_nominal)", nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
"", group.second.second, doc, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
}

iEvent.put(std::move(lheWeightTables));
Expand Down Expand Up @@ -131,13 +138,15 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::RunCache<std
edm::ParameterSetDescription desc;
desc.add<edm::InputTag>("lheInfo", {"externalLHEProducer"})
->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)");
desc.add<std::vector<std::string>>("weightgroups");
desc.add<int32_t>("lheWeightPrecision", -1)->setComment("Number of bits in the mantissa for LHE weights");
descriptions.addDefault(desc);
}

protected:
const edm::InputTag lheInputTag_;
const edm::EDGetTokenT<LHEEventProduct> lheToken_;
const std::vector<std::string> weightgroups_;
int lheWeightPrecision_;
};

Expand Down
1 change: 0 additions & 1 deletion PhysicsTools/NanoAOD/plugins/NanoAODOutputModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ void NanoAODOutputModule::openFile(edm::FileBlock const&) {
m_runTables.clear();
const auto& keeps = keptProducts();
for (const auto& keep : keeps[edm::InEvent]) {
std::cout << keep.first->className() << std::endl;
if (keep.first->className() == "nanoaod::FlatTable") {
m_tableTokens.emplace_back(keep.second);
} else if (keep.first->className() == "std::vector<nanoaod::FlatTable>") {
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
lheWeightsTable = cms.EDProducer(
"LHEWeightsTableProducer",
lheInfo = cms.InputTag("externalLHEProducer"),
weightgroups = cms.vstring(["mg_reweighting"]),
lheWeightPrecision = cms.int32(14),
)

Expand Down

0 comments on commit d663d2f

Please sign in to comment.