From 3180b59bfee7660535dce9e9142ba890c1319b33 Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Sat, 1 Aug 2020 23:36:20 +0200 Subject: [PATCH] Don't fail if there are no LHE weights --- .../plugins/LHEWeightsTableProducer.cc | 45 +++++++++++-------- PhysicsTools/NanoAOD/python/nanogen_cff.py | 2 +- PhysicsTools/NanoAOD/test/testNanoWeights.py | 5 ++- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc index 9f6bd55db7bca..58c883ee00995 100644 --- a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc +++ b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc @@ -48,25 +48,32 @@ class LHEWeightsTableProducer : public edm::global::EDProducer lheWeightHandle; - for (auto& token : lheWeightTokens_) { - iEvent.getByToken(token, lheWeightHandle); - if (lheWeightHandle.isValid()) { - break; + bool foundLheWeights = false; + if (foundLheWeights) { + for (auto& token : lheWeightTokens_) { + iEvent.getByToken(token, lheWeightHandle); + if (lheWeightHandle.isValid()) { + break; + } } } - const GenWeightProduct* lheWeightProduct = lheWeightHandle.product(); - WeightsContainer lheWeights = lheWeightProduct->weights(); + WeightsContainer lheWeights; + if (foundLheWeights) { + const GenWeightProduct* lheWeightProduct = lheWeightHandle.product(); + lheWeights = lheWeightProduct->weights(); + } edm::Handle genWeightHandle; iEvent.getByToken(genWeightToken_, genWeightHandle); const GenWeightProduct* genWeightProduct = genWeightHandle.product(); WeightsContainer genWeights = genWeightProduct->weights(); - auto lheWeightTables = std::make_unique>(); auto const& weightInfos = *luminosityBlockCache(iEvent.getLuminosityBlock().index()); - addWeightGroupToTable(lheWeightTables, "LHE", weightInfos.at(inLHE), lheWeights); + auto lheWeightTables = std::make_unique>(); + if (foundLheWeights) + addWeightGroupToTable(lheWeightTables, "LHE", weightInfos.at(inLHE), lheWeights); addWeightGroupToTable(lheWeightTables, "Gen", weightInfos.at(inGen), genWeights); iEvent.put(std::move(lheWeightTables)); @@ -76,14 +83,13 @@ class LHEWeightsTableProducer : public edm::global::EDProducer typeCount = {}; + for (auto& type : gen::allWeightTypes) + typeCount[type] = 0; for (const auto& groupInfo : weightInfos) { std::string entryName = typeName; gen::WeightType weightType = groupInfo.group->weightType(); - if (previousType != weightType) - typeCount = 0; std::string name = weightTypeNames_.at(weightType); std::string label = groupInfo.group->name(); @@ -102,17 +108,16 @@ class LHEWeightsTableProducer : public edm::global::EDProducer 0) { + if (typeCount[weightType] > 0) { entryName.append("AltSet"); - entryName.append(std::to_string(typeCount)); + entryName.append(std::to_string(typeCount[weightType])); } lheWeightTables->emplace_back(weights.size(), entryName, false); lheWeightTables->back().addColumn( "", weights, label, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_); - typeCount++; - previousType = weightType; + typeCount[weightType]++; } } @@ -120,10 +125,12 @@ class LHEWeightsTableProducer : public edm::global::EDProducer lheWeightInfoHandle; for (auto& token : lheWeightInfoTokens_) { iLumi.getByToken(token, lheWeightInfoHandle); if (lheWeightInfoHandle.isValid()) { + foundLheWeights = true; break; } } @@ -137,8 +144,10 @@ class LHEWeightsTableProducer : public edm::global::EDProducer