Skip to content

Commit

Permalink
Working implementation producing new prod if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Jan 17, 2020
1 parent 6aaef39 commit 6ccee5d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
24 changes: 16 additions & 8 deletions GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit
std::string lheLabel_;
edm::EDGetTokenT<LHERunInfoProduct> lheRunInfoToken_;
edm::EDGetTokenT<LHEEventProduct> lheEventToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> lheWeightInfoToken_;
bool foundWeightProduct_;

void produce(edm::Event&, const edm::EventSetup&) override;
void beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) override;
Expand All @@ -48,11 +50,10 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit
// constructors and destructor
//
LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iConfig) :
lheLabel_(iConfig.getUntrackedParameter<std::string>("lheSourceLabel")),
lheRunInfoToken_(consumes<LHERunInfoProduct, edm::InRun>(
iConfig.getUntrackedParameter<edm::InputTag>("lheSource", edm::InputTag("externalLHEProducer")))),
lheEventToken_(consumes<LHEEventProduct>(
iConfig.getUntrackedParameter<edm::InputTag>("lheSource", edm::InputTag("externalLHEProducer"))))
lheLabel_(iConfig.getParameter<std::string>("lheSourceLabel")),
lheRunInfoToken_(consumes<LHERunInfoProduct, edm::InRun>(lheLabel_)),
lheEventToken_(consumes<LHEEventProduct>(lheLabel_)),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(lheLabel_))
{
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
Expand All @@ -67,19 +68,19 @@ LHEWeightProductProducer::~LHEWeightProductProducer()
// ------------ method called to produce the data ------------
void
LHEWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
if (foundWeightProduct_)
return;

edm::Handle<LHEEventProduct> lheEventInfo;
iEvent.getByToken(lheEventToken_, lheEventInfo);
// Read weights from LHEEventProduct
auto weightProduct = weightHelper_.weightProduct(lheEventInfo->weights(), lheEventInfo->originalXWGTUP());
iEvent.put(std::move(weightProduct));
}

// ------------ method called when starting to processes a run ------------
void
LHEWeightProductProducer::beginRun(edm::Run const& run, edm::EventSetup const& es) {
edm::Handle<LHERunInfoProduct> lheRunInfoHandle;
//run.getByToken(lheRunInfoToken_, lheRunInfoHandle);
// get by token gives an error (the same one that's been in the ExternalLHEProducer for ages)
run.getByLabel(lheLabel_, lheRunInfoHandle);

typedef std::vector<LHERunInfoProduct::Header>::const_iterator header_cit;
Expand All @@ -97,6 +98,13 @@ LHEWeightProductProducer::endRun(edm::Run const& run, edm::EventSetup const& es)

void
LHEWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) {
edm::Handle<GenWeightInfoProduct> lheWeightInfoHandle;
lumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle);
if (lheWeightInfoHandle.isValid()) {
foundWeightProduct_ = true;
return;
}

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
Expand Down
29 changes: 21 additions & 8 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ namespace {
class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBlockCache<WeightGroupsToStore>> {
public:
LHEWeightsTableProducer(edm::ParameterSet const& params) :
lheWeightToken_(consumes<GenWeightProduct>(params.getParameter<edm::InputTag>("lheWeights"))),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(params.getParameter<edm::InputTag>("lheWeights"))),
lheWeightTokens_(edm::vector_transform(params.getParameter<std::vector<edm::InputTag>>("lheWeights"),
[this](const edm::InputTag& tag) { return mayConsume<GenWeightProduct>(tag); })),
lheWeightInfoTokens_(edm::vector_transform(params.getParameter<std::vector<edm::InputTag>>("lheWeights"),
[this](const edm::InputTag& tag) { return mayConsume<GenWeightInfoProduct, edm::InLumi>(tag); })),
genWeightToken_(consumes<GenWeightProduct>(params.getParameter<edm::InputTag>("genWeights"))),
genWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(params.getParameter<edm::InputTag>("genWeights"))),
weightgroups_(edm::vector_transform(params.getParameter<std::vector<std::string>>("weightgroups"),
Expand All @@ -43,9 +45,14 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
}

void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override {
// tables for LHE weights, may not be filled
edm::Handle<GenWeightProduct> lheWeightHandle;
iEvent.getByToken(lheWeightToken_, lheWeightHandle);
for (auto& token : lheWeightTokens_) {
iEvent.getByToken(token, lheWeightHandle);
if (lheWeightHandle.isValid()) {
break;
}
}

const GenWeightProduct* lheWeightProduct = lheWeightHandle.product();
WeightsContainer lheWeights = lheWeightProduct->weights();

Expand Down Expand Up @@ -113,7 +120,12 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
// Set equal to the max number of groups
// subtrack 1 for each weight group you find
edm::Handle<GenWeightInfoProduct> lheWeightInfoHandle;
iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle);
for (auto& token : lheWeightInfoTokens_) {
iLumi.getByToken(token, lheWeightInfoHandle);
if (lheWeightInfoHandle.isValid()) {
break;
}
}

edm::Handle<GenWeightInfoProduct> genWeightInfoHandle;
iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle);
Expand Down Expand Up @@ -173,7 +185,8 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<edm::InputTag>("lheWeights");
desc.add<std::vector<edm::InputTag>>("lheWeights");
//desc.add<std::vector<edm::InputTag>>("genWeights");
desc.add<edm::InputTag>("genWeights");
desc.add<std::vector<std::string>>("weightgroups");
desc.add<std::vector<int>>("maxGroupsPerType");
Expand All @@ -184,8 +197,8 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl

protected:
const edm::EDGetTokenT<LHEEventProduct> lheToken_;
const edm::EDGetTokenT<GenWeightProduct> lheWeightToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> lheWeightInfoToken_;
const std::vector<edm::EDGetTokenT<GenWeightProduct>> lheWeightTokens_;
const std::vector<edm::EDGetTokenT<GenWeightInfoProduct>> lheWeightInfoTokens_;
const edm::EDGetTokenT<GenWeightProduct> genWeightToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> genWeightInfoToken_;
const std::vector<gen::WeightType> weightgroups_;
Expand Down
7 changes: 6 additions & 1 deletion PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
genWeights = cms.EDProducer("GenWeightProductProducer",
genInfo = cms.InputTag("generator"))

lheWeights = cms.EDProducer("LHEWeightProductProducer",
lheSourceLabel = cms.string("externalLHEProducer"),
lheSource = cms.InputTag("externalLHEProducer"))

lheWeightsTable = cms.EDProducer(
"LHEWeightsTableProducer",
lheWeights = cms.InputTag("externalLHEProducer"),
lheWeights = cms.VInputTag(["externalLHEProducer", "lheWeights"]),
genWeights = cms.InputTag("genWeights"),
# Warning: you can use a full string, but only the first character is read.
# Note also that the capitalization is important! For example, 'parton shower'
Expand Down Expand Up @@ -44,6 +48,7 @@

nanogenSequence = cms.Sequence(
genWeights+
lheWeights+
nanoMetadata+
particleLevel+
genJetTable+
Expand Down

0 comments on commit 6ccee5d

Please sign in to comment.