From 6ccee5d97cb552ffbad85a773e833fc8e6ef658b Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Fri, 17 Jan 2020 20:57:33 +0100 Subject: [PATCH] Working implementation producing new prod if needed --- .../Core/plugins/LHEWeightProductProducer.cc | 24 ++++++++++----- .../plugins/LHEWeightsTableProducer.cc | 29 ++++++++++++++----- PhysicsTools/NanoAOD/python/nanogen_cff.py | 7 ++++- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc b/GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc index a4f0836314460..db74a3b25730b 100644 --- a/GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc +++ b/GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc @@ -36,6 +36,8 @@ class LHEWeightProductProducer : public edm::one::EDProducer lheRunInfoToken_; edm::EDGetTokenT lheEventToken_; + const edm::EDGetTokenT lheWeightInfoToken_; + bool foundWeightProduct_; void produce(edm::Event&, const edm::EventSetup&) override; void beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) override; @@ -48,11 +50,10 @@ class LHEWeightProductProducer : public edm::one::EDProducer("lheSourceLabel")), - lheRunInfoToken_(consumes( - iConfig.getUntrackedParameter("lheSource", edm::InputTag("externalLHEProducer")))), - lheEventToken_(consumes( - iConfig.getUntrackedParameter("lheSource", edm::InputTag("externalLHEProducer")))) + lheLabel_(iConfig.getParameter("lheSourceLabel")), + lheRunInfoToken_(consumes(lheLabel_)), + lheEventToken_(consumes(lheLabel_)), + lheWeightInfoToken_(consumes(lheLabel_)) { produces(); produces(); @@ -67,6 +68,9 @@ LHEWeightProductProducer::~LHEWeightProductProducer() // ------------ method called to produce the data ------------ void LHEWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) { + if (foundWeightProduct_) + return; + edm::Handle lheEventInfo; iEvent.getByToken(lheEventToken_, lheEventInfo); // Read weights from LHEEventProduct @@ -74,12 +78,9 @@ LHEWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSe 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 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::const_iterator header_cit; @@ -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 lheWeightInfoHandle; + lumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle); + if (lheWeightInfoHandle.isValid()) { + foundWeightProduct_ = true; + return; + } + auto weightInfoProduct = std::make_unique(); for (auto& weightGroup : weightHelper_.weightGroups()) { weightInfoProduct->addWeightGroupInfo(weightGroup.clone()); diff --git a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc index ed26af86897bc..5587cbd3d31e6 100644 --- a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc +++ b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc @@ -28,8 +28,10 @@ namespace { class LHEWeightsTableProducer : public edm::global::EDProducer> { public: LHEWeightsTableProducer(edm::ParameterSet const& params) : - lheWeightToken_(consumes(params.getParameter("lheWeights"))), - lheWeightInfoToken_(consumes(params.getParameter("lheWeights"))), + lheWeightTokens_(edm::vector_transform(params.getParameter>("lheWeights"), + [this](const edm::InputTag& tag) { return mayConsume(tag); })), + lheWeightInfoTokens_(edm::vector_transform(params.getParameter>("lheWeights"), + [this](const edm::InputTag& tag) { return mayConsume(tag); })), genWeightToken_(consumes(params.getParameter("genWeights"))), genWeightInfoToken_(consumes(params.getParameter("genWeights"))), weightgroups_(edm::vector_transform(params.getParameter>("weightgroups"), @@ -43,9 +45,14 @@ class LHEWeightsTableProducer : public edm::global::EDProducer 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(); @@ -113,7 +120,12 @@ class LHEWeightsTableProducer : public edm::global::EDProducer lheWeightInfoHandle; - iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle); + for (auto& token : lheWeightInfoTokens_) { + iLumi.getByToken(token, lheWeightInfoHandle); + if (lheWeightInfoHandle.isValid()) { + break; + } + } edm::Handle genWeightInfoHandle; iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle); @@ -173,7 +185,8 @@ class LHEWeightsTableProducer : public edm::global::EDProducer("lheWeights"); + desc.add>("lheWeights"); + //desc.add>("genWeights"); desc.add("genWeights"); desc.add>("weightgroups"); desc.add>("maxGroupsPerType"); @@ -184,8 +197,8 @@ class LHEWeightsTableProducer : public edm::global::EDProducer lheToken_; - const edm::EDGetTokenT lheWeightToken_; - const edm::EDGetTokenT lheWeightInfoToken_; + const std::vector> lheWeightTokens_; + const std::vector> lheWeightInfoTokens_; const edm::EDGetTokenT genWeightToken_; const edm::EDGetTokenT genWeightInfoToken_; const std::vector weightgroups_; diff --git a/PhysicsTools/NanoAOD/python/nanogen_cff.py b/PhysicsTools/NanoAOD/python/nanogen_cff.py index b79d0d61c0b39..88d8ebe03c18f 100644 --- a/PhysicsTools/NanoAOD/python/nanogen_cff.py +++ b/PhysicsTools/NanoAOD/python/nanogen_cff.py @@ -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' @@ -44,6 +48,7 @@ nanogenSequence = cms.Sequence( genWeights+ + lheWeights+ nanoMetadata+ particleLevel+ genJetTable+