Skip to content

Commit

Permalink
LHEWeightsProducer doesn't fail if no LHE weights
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Aug 3, 2020
1 parent c5fa5ed commit d057424
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
58 changes: 42 additions & 16 deletions GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "GeneratorInterface/Core/interface/LHEWeightHelper.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/transform.h"

class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer, edm::one::WatchRuns> {
public:
Expand All @@ -32,11 +33,12 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit

private:
gen::LHEWeightHelper weightHelper_;
std::string lheLabel_;
edm::EDGetTokenT<LHERunInfoProduct> lheRunInfoToken_;
edm::EDGetTokenT<LHEEventProduct> lheEventToken_;
const edm::EDGetTokenT<GenWeightInfoProduct> lheWeightInfoToken_;
std::vector<std::string> lheLabels_;
std::vector<edm::EDGetTokenT<LHEEventProduct>> lheEventTokens_;
std::vector<edm::EDGetTokenT<LHERunInfoProduct>> lheRunInfoTokens_;
std::vector<edm::EDGetTokenT<GenWeightInfoProduct>> lheWeightInfoTokens_;
bool foundWeightProduct_ = false;
bool hasLhe_ = false;

void produce(edm::Event&, const edm::EventSetup&) override;
void beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) override;
Expand All @@ -46,10 +48,13 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit

// TODO: Accept a vector of strings (source, externalLHEProducer) exit if neither are found
LHEWeightProductProducer::LHEWeightProductProducer(const edm::ParameterSet& iConfig)
: lheLabel_(iConfig.getParameter<std::string>("lheSourceLabel")),
lheRunInfoToken_(consumes<LHERunInfoProduct, edm::InRun>(lheLabel_)),
lheEventToken_(consumes<LHEEventProduct>(lheLabel_)),
lheWeightInfoToken_(consumes<GenWeightInfoProduct, edm::InLumi>(lheLabel_)) {
: lheLabels_(iConfig.getParameter<std::vector<std::string>>("lheSourceLabels")),
lheEventTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<LHEEventProduct>(tag); })),
lheRunInfoTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<LHERunInfoProduct, edm::InRun>(tag); })),
lheWeightInfoTokens_(edm::vector_transform(lheLabels_,
[this](const std::string& tag) { return mayConsume<GenWeightInfoProduct, edm::InLumi>(tag); })) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
}
Expand All @@ -58,19 +63,33 @@ LHEWeightProductProducer::~LHEWeightProductProducer() {}

// ------------ method called to produce the data ------------
void LHEWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
if (foundWeightProduct_)
if (foundWeightProduct_ || !hasLhe_)
return;

edm::Handle<LHEEventProduct> lheEventInfo;
iEvent.getByToken(lheEventToken_, lheEventInfo);
// Read weights from LHEEventProduct
for (auto& token : lheEventTokens_) {
iEvent.getByToken(token, lheEventInfo);
if (lheEventInfo.isValid()) {
break;
}
}

auto weightProduct = weightHelper_.weightProduct(lheEventInfo->weights(), lheEventInfo->originalXWGTUP());
iEvent.put(std::move(weightProduct));
}

void LHEWeightProductProducer::beginRun(edm::Run const& run, edm::EventSetup const& es) {
edm::Handle<LHERunInfoProduct> lheRunInfoHandle;
run.getByLabel(lheLabel_, lheRunInfoHandle);
for (auto& label : lheLabels_) {
run.getByLabel(label, lheRunInfoHandle);
if (lheRunInfoHandle.isValid()) {
hasLhe_ = true;
break;
}
}
if (!hasLhe_)
return;


typedef std::vector<LHERunInfoProduct::Header>::const_iterator header_cit;
LHERunInfoProduct::Header headerWeightInfo;
Expand All @@ -88,11 +107,18 @@ void LHEWeightProductProducer::endRun(edm::Run const& run, edm::EventSetup const

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

for (auto& token : lheWeightInfoTokens_) {
lumi.getByToken(token, lheWeightInfoHandle);
if (lheWeightInfoHandle.isValid()) {
foundWeightProduct_ = true;
return;
}
}

if (!hasLhe_)
return;

weightHelper_.parseWeights();

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
Expand Down
2 changes: 1 addition & 1 deletion GeneratorInterface/Core/src/GenWeightHelper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace gen {
std::vector<std::string> subInfo;
boost::split(subInfo, i, boost::is_any_of("="));
if (subInfo.size() == 2) {
attributes[trim(subInfo[0])] = trim(subInfo[1]);
attributes[boost::algorithm::trim_copy(subInfo[0])] = boost::algorithm::trim_copy(subInfo[1]);
}
}
if (attributes["group"] != curGroup) {
Expand Down
3 changes: 0 additions & 3 deletions PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl

auto lheWeightTables = std::make_unique<std::vector<nanoaod::FlatTable>>();
if (foundLheWeights) {
std::cout << "Adding LHEweights\n";
addWeightGroupToTable(lheWeightTables, "LHE", weightInfos.at(inLHE), lheWeights);
}
addWeightGroupToTable(lheWeightTables, "Gen", weightInfos.at(inGen), genWeights);
Expand All @@ -88,8 +87,6 @@ class LHEWeightsTableProducer : public edm::global::EDProducer<edm::LuminosityBl
for (auto& type : gen::allWeightTypes)
typeCount[type] = 0;

std::cout << "Adding type " << typeName << std::endl;

for (const auto& groupInfo : weightInfos) {
std::string entryName = typeName;
gen::WeightType weightType = groupInfo.group->weightType();
Expand Down
7 changes: 4 additions & 3 deletions PhysicsTools/NanoAOD/python/nanogen_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
genLumiInfoHeader = cms.InputTag("generator"))

lheWeights = cms.EDProducer("LHEWeightProductProducer",
# TODO: Should take a vstring of inputs
lheSourceLabel = cms.string("externalLHEProducer"))
lheSourceLabels = cms.vstring(["externalLHEProducer", "source"])
#lheWeightSourceLabels = cms.vstring(["externalLHEProducer", "source"])
)

lheWeightsTable = cms.EDProducer(
"LHEWeightsTableProducer",
Expand Down Expand Up @@ -78,7 +79,7 @@

nanogenMiniSequence = cms.Sequence(
genWeights+
#lheWeights+
lheWeights+
nanoMetadata+
mergedGenParticles+
genParticles2HepMC+
Expand Down

0 comments on commit d057424

Please sign in to comment.