Skip to content

Commit

Permalink
Close to working, but segfaulting
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Jan 10, 2020
1 parent e542eaa commit ca22fc5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
1 change: 1 addition & 0 deletions GeneratorInterface/Core/test/dumpWeightInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
print "-"*10, "Looking at entry", j, "length is", len(weights),"-"*10
matching = weightInfoProd.orderedWeightGroupInfo(j)
print "Group is", matching, "name is", matching.name(), "well formed?", matching.isWellFormed()
print type(matching.weightType()), matching.weightType()
if matching.weightType() == 1:
for var in [(x, y) for x in ["05", "1", "2"] for y in ["05", "1", "2"]]:
name = "muR%smuF%sIndex" % (var[0], var[1]) if not (var[0] == "1" and var[1] == "1") else "centralIndex"
Expand Down
87 changes: 61 additions & 26 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@
#include <tinyxml2.h>

namespace {
typedef std::unordered_map<std::string, gen::WeightGroupData> WeightGroupsToStore;
typedef std::pair<std::vector<gen::WeightGroupData>, std::vector<gen::WeightGroupData>> WeightGroupsToStore;
//struct WeightGroupToStore {
// gen::WeightType type;
// std::string name;
// bool inLHE;
// gen::WeightGroupData groupInfo;
//}
} // namespace

class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBlockCache<WeightGroupsToStore>> {
public:
LHEWeightsTableProducer(edm::ParameterSet const& params)
: lheInputTag_(params.getParameter<edm::InputTag>("lheInfo")),
lheToken_(consumes<LHEEventProduct>(params.getParameter<edm::InputTag>("lheInfo"))),
lheWeightToken_(consumes<GenWeightProduct>(params.getParameter<edm::InputTag>("lheInfo"))),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(params.getParameter<edm::InputTag>("lheInfo"))),
weightgroups_(params.getParameter<std::vector<std::string>>("weightgroups")),
lheWeightToken_(consumes<GenWeightProduct>(params.getParameter<edm::InputTag>("lheWeights"))),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(params.getParameter<edm::InputTag>("lheWeights"))),
genWeightToken_(consumes<GenWeightProduct>(params.getParameter<edm::InputTag>("genWeights"))),
genWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(params.getParameter<edm::InputTag>("genWeights"))),
//weightgroups_(params.getParameter<std::vector<int>>("weightgroups")),
lheWeightPrecision_(params.getParameter<int32_t>("lheWeightPrecision")) {
//consumes<LHERunInfoProduct, edm::InRun>(lheInputTag_);
produces<std::vector<nanoaod::FlatTable>>();
}

Expand All @@ -45,56 +52,75 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
const GenWeightProduct* lheWeightProduct = lheWeightHandle.product();
WeightsContainer lheWeights = lheWeightProduct->weights();

auto lheWeightTables = std::make_unique<std::vector<nanoaod::FlatTable>>();
auto const& weightInfos = *luminosityBlockCache(iEvent.getLuminosityBlock().index());
edm::Handle<GenWeightProduct> genWeightHandle;
iEvent.getByToken(genWeightToken_, genWeightHandle);
const GenWeightProduct* genWeightProduct = genWeightHandle.product();
WeightsContainer genWeights = genWeightProduct->weights();

auto lheWeightTables = std::make_unique<std::vector<nanoaod::FlatTable>>();
double w0 = lheInfo.originalXWGTUP();
auto const& weightInfos = *luminosityBlockCache(iEvent.getLuminosityBlock().index());

addWeightGroupToTable("Scale", lheWeightTables, weightInfos, lheWeights, w0);
addWeightGroupToTable("MEParam", lheWeightTables, weightInfos, lheWeights, w0);
addWeightGroupToTable("Pdf", lheWeightTables, weightInfos, lheWeights, w0);

addWeightGroupToTable(lheWeightTables, "Lhe", weightInfos.first, lheWeights, w0);
addWeightGroupToTable(lheWeightTables, "Gen", weightInfos.second, genWeights, w0);

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

void addWeightGroupToTable(std::string name, std::unique_ptr<std::vector<nanoaod::FlatTable>>& lheWeightTables,
const WeightGroupsToStore& weightInfos, WeightsContainer& lheWeights, double w0) const {
if (weightInfos.count(name)) {
const auto& groupInfo = weightInfos.at(name);
auto weights = groupInfo.group->weightType() != gen::kScaleWeights ? normalizedWeights(lheWeights, groupInfo, w0) :
void addWeightGroupToTable(std::unique_ptr<std::vector<nanoaod::FlatTable>>& lheWeightTables, std::string entryName,
const std::vector<gen::WeightGroupData>& weightInfos, WeightsContainer& lheWeights, double w0) const {
size_t typeCount = 0;
gen::WeightType currentGroup = gen::kUnknownWeights;

for (const auto& groupInfo : weightInfos) {
gen::WeightType weightType = groupInfo.group->weightType();
std::string name = weightTypeNames_.at(weightType);
auto weights = weightType != gen::kScaleWeights ? normalizedWeights(lheWeights, groupInfo, w0) :
orderedScaleWeights(lheWeights, groupInfo, w0);
std::string entryName = "LHE";
if (typeCount > 0)
entryName.append(std::to_string(typeCount));
entryName.append(name);
entryName.append("Weight");

lheWeightTables->emplace_back(weights.size(), entryName, false);
lheWeightTables->back().addColumn<float>(
"", weights, weightInfos.at(name).group->name(), nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);
"", weights, groupInfo.group->name(), nanoaod::FlatTable::FloatColumn, lheWeightPrecision_);

typeCount++;
if (currentGroup != weightType)
typeCount = 0;
currentGroup = weightType;
}
}


std::shared_ptr<WeightGroupsToStore> globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi,
edm::EventSetup const&) const override {

// Set equal to the max number of groups
// subtrack 1 for each weight group you find
edm::Handle<GenWeightInfoProduct> lheWeightInfoHandle;
iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle);

edm::Handle<GenWeightInfoProduct> genWeightInfoHandle;
iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle);

auto scaleGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kScaleWeights);
auto meGroups = lheWeightInfoHandle->weightGroupsAndIndicesByType(gen::kMEParamWeights);

WeightGroupsToStore weightsToStore;
weightsToStore.insert({"Scale", scaleGroups.front()});
weightsToStore.first.insert(weightsToStore.first.end(), scaleGroups.begin(), scaleGroups.end());
weightsToStore.first.insert(weightsToStore.first.end(), meGroups.begin(), meGroups.end());

for (auto lhaid : {306000, 29000}) {
for (auto lhaid : {306000, 91400, 260000}) {
if (auto pdfGroup = lheWeightInfoHandle->pdfGroupWithIndexByLHAID(lhaid)) {
weightsToStore.insert({"Pdf", pdfGroup.value()});
weightsToStore.first.push_back(pdfGroup.value());
break;
}
}

if (meGroups.size() > 0)
weightsToStore.insert({"MEParam", meGroups.front()});
auto psGroups = genWeightInfoHandle->weightGroupsAndIndicesByType(gen::kPartonShowerWeights);
weightsToStore.second.insert(weightsToStore.second.end(), psGroups.begin(), psGroups.end());

return std::make_shared<WeightGroupsToStore>(weightsToStore);
}
Expand Down Expand Up @@ -135,7 +161,9 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
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<std::vector<int>>("weightgroups");
desc.add<edm::InputTag>("lheWeights");
desc.add<edm::InputTag>("genWeights");
desc.add<int32_t>("lheWeightPrecision", -1)->setComment("Number of bits in the mantissa for LHE weights");
descriptions.addDefault(desc);
}
Expand All @@ -145,8 +173,15 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
const edm::EDGetTokenT<LHEEventProduct> lheToken_;
const edm::EDGetTokenT<GenWeightProduct> lheWeightToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> lheWeightInfoToken_;

const std::vector<std::string> weightgroups_;
const edm::EDGetTokenT<GenWeightProduct> genWeightToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> genWeightInfoToken_;
//const std::vector<gen::WeightType> weightgroups_;
const std::unordered_map<gen::WeightType, std::string> weightTypeNames_ = {
{gen::kScaleWeights, "Scale"},
{gen::kPdfWeights, "Pdf"},
{gen::kMEParamWeights, "MEParam"},
{gen::kUnknownWeights, "Unknown"},
};

//std::unordered_map<std::string, int> weightGroupIndices_;
int lheWeightPrecision_;
Expand Down
12 changes: 10 additions & 2 deletions PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
from PhysicsTools.NanoAOD.particlelevel_cff import *
from PhysicsTools.NanoAOD.lheInfoTable_cfi import *
from PhysicsTools.NanoAOD.genWeightsTable_cfi import *
import ROOT

genWeights = cms.EDProducer("GenWeightProductProducer")

lheWeightsTable = cms.EDProducer(
"LHEWeightsTableProducer",
lheInfo = cms.InputTag("externalLHEProducer"),
#lheWeights = cms.InputTag("externalLHEProducer"),
weightgroups = cms.vstring(["mg_reweighting"]),
lheWeights = cms.InputTag("externalLHEProducer"),
genWeights = cms.InputTag("genWeights"),
#weightgroups = cms.vint32([ROOT.gen.kScaleWeights, ROOT.gen.kMEParamWeights, ROOT.gen.kPdfWeights, ROOT.UnknownWeights]),
#weightgroups = cms.vint32([0,1,2,3,4]),
#numWeightgroups = cms.vint([1, -1, 1, 2, 1]),
#pdfs = cms.vint([91400, 306000, 260000]),
lheWeightPrecision = cms.int32(14),
)

Expand All @@ -33,6 +40,7 @@
)

nanogenSequence = cms.Sequence(
genWeights+
nanoMetadata+
particleLevel+
genJetTable+
Expand Down
2 changes: 1 addition & 1 deletion PhysicsTools/NanoAOD/test/nanoGen_test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
process.genfiltersummary_step = cms.EndPath(process.genFilterSummary)

# Path and EndPath definitions
process.nanoGEN_step = cms.Path(process.nanogenSequence)
process.nanoGEN_step = cms.Path(process.genWeights*process.nanogenSequence)
process.endjob_step = cms.EndPath(process.endOfProcess)
process.NANOAODGENoutput_step = cms.EndPath(process.NANOAODGENoutput)

Expand Down

0 comments on commit ca22fc5

Please sign in to comment.