From a91bdf2d849587546a22f0274b98541fc7e75ecb Mon Sep 17 00:00:00 2001 From: Suvankar Roy Chowdhury Date: Fri, 21 Aug 2020 19:26:39 +0200 Subject: [PATCH] updates to allow saving weight sums from all weight categories --- .../NanoAOD/interface/GenWeightCounters.h | 119 +++++ .../plugins/LHEWeightsTableProducer.cc | 498 +++++++++++------- 2 files changed, 440 insertions(+), 177 deletions(-) create mode 100644 PhysicsTools/NanoAOD/interface/GenWeightCounters.h diff --git a/PhysicsTools/NanoAOD/interface/GenWeightCounters.h b/PhysicsTools/NanoAOD/interface/GenWeightCounters.h new file mode 100644 index 0000000000000..15ac334b2e863 --- /dev/null +++ b/PhysicsTools/NanoAOD/interface/GenWeightCounters.h @@ -0,0 +1,119 @@ +#ifndef GENWEIGHTSCOUNTERS_h +#define GENWEIGHTSCOUNTERS_h +#include +#include +#include +namespace genCounter { + + void mergeSumVectors(std::vector& v1, std::vector const& v2) { + if (v1.empty() && !v2.empty()) + v1.resize(v2.size(), 0); + if (!v2.empty()) + for (unsigned int i = 0, n = v1.size(); i < n; ++i) + v1[i] += v2[i]; + } + + /// ---- Cache object for running sums of weights ---- + class Counter { + public: + void clear() { + num_ = 0; + sumw_ = 0; + sumw2_ = 0; + weightSumMap_.clear(); + } + + // inc only the gen counters + void incGenOnly(double w) { + num_++; + sumw_ += w; + sumw2_ += (w * w); + } + + void incLHE(double w0, const std::vector& weightV, const std::string& wName) { + //if new type of weight, create a map element + if(weightSumMap_.find(wName) == weightSumMap_.end()) { + std::vector temp; + weightSumMap_.insert({wName, temp}); + } + if (!weightV.empty()) { + if (weightSumMap_[wName].empty()) + weightSumMap_[wName].resize(weightV.size(), 0); + for (unsigned int i = 0, n = weightV.size(); i < n; ++i) + weightSumMap_[wName][i] += (w0 * weightV[i]); + } + // incGenOnly(w0); + //incPSOnly(w0, wPS); + } + + void mergeSumMap(const Counter& other) { + num_ += other.num_; + sumw_ += other.sumw_; + sumw2_ += other.sumw2_; + //if weightMap_ for "this" is empty, create map elements with empty + //vectors before merging + if(weightSumMap_.empty() && !other.weightSumMap_.empty()) { + for(auto& wmap : other.weightSumMap_) { + std::vector temp; + weightSumMap_.insert({wmap.first, temp}); + } + } + for(auto& wmap : weightSumMap_) { + mergeSumVectors(wmap.second, other.weightSumMap_.at(wmap.first)); + } + /* + std::cout << "From merge function\n"; + for(auto& wmap : weightSumMap_) { + std::cout << "Wname:" << wmap.first + << "\t WVec Size:" << wmap.second.size() + << "\t First entry:" << wmap.second.at(0) + << std::endl;; + } + std::cout << "End : From merge function\n"; + */ + } + + //private: + // the counters + long long num_ = 0; + long double sumw_ = 0; + long double sumw2_ = 0; + std::map> weightSumMap_; + }; + + struct CounterMap { + std::map countermap; + Counter* active_el = nullptr; + std::string active_label = ""; + + void mergeSumMap(const CounterMap& other) { + for (const auto& y : other.countermap) + countermap[y.first].mergeSumMap(y.second); + active_el = nullptr; + } + + void clear() { + for (auto x : countermap) + x.second.clear(); + } + + void setLabel(std::string label) { + active_el = &(countermap[label]); + active_label = label; + } + void checkLabelSet() { + if (!active_el) + throw cms::Exception("LogicError", "Called CounterMap::get() before setting the active label\n"); + } + Counter* get() { + checkLabelSet(); + return active_el; + } + std::string& getLabel() { + checkLabelSet(); + return active_label; + } + }; + +} +#endif diff --git a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc index 56ff5f4f66ada..53eb1c388e111 100644 --- a/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc +++ b/PhysicsTools/NanoAOD/plugins/LHEWeightsTableProducer.cc @@ -3,18 +3,21 @@ #include "FWCore/Framework/interface/Run.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "DataFormats/NanoAOD/interface/FlatTable.h" +#include "DataFormats/NanoAOD/interface/MergeableCounterTable.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h" #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h" #include "SimDataFormats/GeneratorProducts/interface/GenWeightInfoProduct.h" +#include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h" #include "SimDataFormats/GeneratorProducts/interface/GenWeightProduct.h" #include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h" #include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h" #include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h" #include "FWCore/Utilities/interface/transform.h" - +#include "PhysicsTools/NanoAOD/interface/GenWeightCounters.h" #include #include #include @@ -23,207 +26,348 @@ namespace { typedef std::vector WeightGroupDataContainer; typedef std::array, 2> WeightGroupsToStore; } // namespace +using CounterMap = genCounter::CounterMap; +using Counter = genCounter::Counter; -class LHEWeightsTableProducer : public edm::global::EDProducer> { +class LHEWeightsTableProducer : +public edm::global::EDProducer, + edm::StreamCache, + edm::RunSummaryCache, + edm::EndRunProducer> { public: - LHEWeightsTableProducer(edm::ParameterSet const& params) - : 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"), - [](auto& c) { return gen::WeightType(c.at(0)); })), - maxGroupsPerType_(params.getParameter>("maxGroupsPerType")), - pdfIds_(params.getUntrackedParameter>("pdfIds", {})), - lheWeightPrecision_(params.getParameter("lheWeightPrecision")) { - if (weightgroups_.size() != maxGroupsPerType_.size()) - throw std::invalid_argument("Inputs 'weightgroups' and 'weightgroupNums' must have equal size"); - produces>(); - } + LHEWeightsTableProducer(edm::ParameterSet const& params); - void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override { - edm::Handle lheWeightHandle; - bool foundLheWeights = false; - for (auto& token : lheWeightTokens_) { - iEvent.getByToken(token, lheWeightHandle); - if (lheWeightHandle.isValid()) { - foundLheWeights = true; - break; - } - } + void produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const override; + //func changed//sroychow + void addWeightGroupToTable(std::unique_ptr>& lheWeightTables, + const char* typeName, + const WeightGroupDataContainer& weightInfos, + WeightsContainer& allWeights, Counter& counter, + double genWeight) const; - WeightsContainer lheWeights; - if (foundLheWeights) { - const GenWeightProduct* lheWeightProduct = lheWeightHandle.product(); - lheWeights = lheWeightProduct->weights(); - } + WeightGroupDataContainer weightDataPerType(edm::Handle& weightsHandle, + gen::WeightType weightType, + int& maxStore) const; - edm::Handle genWeightHandle; - iEvent.getByToken(genWeightToken_, genWeightHandle); - const GenWeightProduct* genWeightProduct = genWeightHandle.product(); - WeightsContainer genWeights = genWeightProduct->weights(); + std::vector orderedScaleWeights(const std::vector& scaleWeights, + const gen::ScaleWeightGroupInfo* scaleGroup) const; - auto const& weightInfos = *luminosityBlockCache(iEvent.getLuminosityBlock().index()); + //Lumiblock + std::shared_ptr 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 + bool foundLheWeights = false; + edm::Handle lheWeightInfoHandle; + for (auto& token : lheWeightInfoTokens_) { + iLumi.getByToken(token, lheWeightInfoHandle); + if (lheWeightInfoHandle.isValid()) { + foundLheWeights = true; + break; + } + } + edm::Handle genWeightInfoHandle; + iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle); + + std::unordered_map storePerType; + for (size_t i = 0; i < weightgroups_.size(); i++) + storePerType[weightgroups_.at(i)] = maxGroupsPerType_.at(i); + + WeightGroupsToStore weightsToStore; + for (auto weightType : gen::allWeightTypes) { + if (foundLheWeights) { + auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]); + weightsToStore.at(inLHE).insert(weightsToStore.at(inLHE).end(), lheWeights.begin(), lheWeights.end()); + } + auto genWeights = weightDataPerType(genWeightInfoHandle, weightType, storePerType[weightType]); + weightsToStore.at(inGen).insert(weightsToStore.at(inGen).end(), genWeights.begin(), genWeights.end()); + } + return std::make_shared(weightsToStore); + } - auto lheWeightTables = std::make_unique>(); - if (foundLheWeights) { - addWeightGroupToTable(lheWeightTables, "LHE", weightInfos.at(inLHE), lheWeights); - } - addWeightGroupToTable(lheWeightTables, "Gen", weightInfos.at(inGen), genWeights); + // nothing to do here + virtual void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const override {} + // create an empty counter + std::unique_ptr beginStream(edm::StreamID) const override { return std::make_unique(); } + // inizialize to zero at begin run + void streamBeginRun(edm::StreamID id, + edm::Run const&, edm::EventSetup const&) const override { streamCache(id)->clear(); } - iEvent.put(std::move(lheWeightTables)); + void streamBeginLuminosityBlock(edm::StreamID id, + edm::LuminosityBlock const& lumiBlock, + edm::EventSetup const& eventSetup) const override { + auto counterMap = streamCache(id); + edm::Handle genLumiInfoHead; + lumiBlock.getByToken(genLumiInfoHeadTag_, genLumiInfoHead); + if (!genLumiInfoHead.isValid()) + edm::LogWarning("LHETablesProducer") + << "No GenLumiInfoHeader product found, will not fill generator model string.\n"; + counterMap->setLabel(genLumiInfoHead.isValid() ? genLumiInfoHead->configDescription() : ""); + //std::cout << "StreamBeginLuminosityBlock:" << id.value() << "\nPrinting counter map keys" << std::endl; + //for(auto& cm : counterMap->countermap) + // std::cout << cm.first << std::endl; + std::string label = genLumiInfoHead.isValid() ? counterMap->getLabel() : "NULL"; + //std::cout << "StreamBeginLuminosityBlock:" << id.value() << "\nCounterMapLabel:" << label << std::endl; + + } + // create an empty counter + std::shared_ptr globalBeginRunSummary(edm::Run const&, edm::EventSetup const&) const override { + return std::make_shared(); } + // add this stream to the summary + void streamEndRunSummary(edm::StreamID id, edm::Run const&, + edm::EventSetup const&, CounterMap* runCounterMap) const override; + // nothing to do per se + void globalEndRunSummary(edm::Run const&, edm::EventSetup const&, CounterMap* runCounterMap) const override {} + // write the total to the run + void globalEndRunProduce(edm::Run& iRun, edm::EventSetup const& es, CounterMap const* runCounterMap) const override; + // nothing to do here + //void globalEndRun(edm::Run const& iRun, edm::EventSetup const& es, CounterMap* runCounterMap) const override {} - void addWeightGroupToTable(std::unique_ptr>& lheWeightTables, - const char* typeName, - const WeightGroupDataContainer& weightInfos, - WeightsContainer& allWeights) const { - std::unordered_map typeCount = {}; - for (auto& type : gen::allWeightTypes) - typeCount[type] = 0; + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); - for (const auto& groupInfo : weightInfos) { - std::string entryName = typeName; - gen::WeightType weightType = groupInfo.group->weightType(); + protected: + //const std::vector> lheTokens_; + const std::vector> lheWeightTokens_; + const std::vector> lheWeightInfoTokens_; + const edm::EDGetTokenT genWeightToken_; + const edm::EDGetTokenT genWeightInfoToken_; + const edm::EDGetTokenT genEventInfoToken_; + const edm::EDGetTokenT genLumiInfoHeadTag_; + const std::vector weightgroups_; + const std::vector maxGroupsPerType_; + const std::vector pdfIds_; + const std::unordered_map weightTypeNames_ = { + {gen::WeightType::kScaleWeights, "Scale"}, + {gen::WeightType::kPdfWeights, "Pdf"}, + {gen::WeightType::kMEParamWeights, "MEParam"}, + {gen::WeightType::kPartonShowerWeights, "PartonShower"}, + {gen::WeightType::kUnknownWeights, "Unknown"}, + }; + + //std::unordered_map weightGroupIndices_; + int lheWeightPrecision_; + enum { inLHE, inGen }; +}; +//put back if needed; till now not used +//lheTokens_(edm::vector_transform(params.getParameter>("lheInfo"), +// [this](const edm::InputTag& tag) { return mayConsume(tag); })), +// +LHEWeightsTableProducer::LHEWeightsTableProducer(edm::ParameterSet const& params) + : 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"))), + genEventInfoToken_(consumes(params.getParameter("genEvent"))), + genLumiInfoHeadTag_(mayConsume(params.getParameter("genLumiInfoHeader"))), + weightgroups_(edm::vector_transform(params.getParameter>("weightgroups"), + [](auto& c) { return gen::WeightType(c.at(0)); })), + maxGroupsPerType_(params.getParameter>("maxGroupsPerType")), + pdfIds_(params.getUntrackedParameter>("pdfIds", {})), + lheWeightPrecision_(params.getParameter("lheWeightPrecision")) +{ + if (weightgroups_.size() != maxGroupsPerType_.size()) + throw std::invalid_argument("Inputs 'weightgroups' and 'weightgroupNums' must have equal size"); + produces>(); + produces(); + produces(); + } - std::string name = weightTypeNames_.at(weightType); - std::string label = groupInfo.group->name(); +void LHEWeightsTableProducer::produce(edm::StreamID id, edm::Event& iEvent, const edm::EventSetup& iSetup) const { - auto& weights = allWeights.at(groupInfo.index); - label.append("; "); - if (false && weightType == gen::WeightType::kScaleWeights && groupInfo.group->isWellFormed() && - groupInfo.group->nIdsContained() < 10) { - weights = orderedScaleWeights(weights, dynamic_cast(groupInfo.group)); - label.append( - "[1] is mur=0.5 muf=1; [2] is mur=0.5 muf=2; [3] is mur=1 muf=0.5 ;" - " [4] is mur=1 muf=1; [5] is mur=1 muf=2; [6] is mur=2 muf=0.5;" - " [7] is mur=2 muf=1 ; [8] is mur=2 muf=2)"); - } else - label.append(groupInfo.group->description()); - - entryName.append(name); - entryName.append("Weight"); - if (typeCount[weightType] > 0) { - entryName.append("AltSet"); - entryName.append(std::to_string(typeCount[weightType])); - } - - lheWeightTables->emplace_back(weights.size(), entryName, false); - lheWeightTables->back().addColumn( - "", weights, label, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_); - - typeCount[weightType]++; - } + //access counter for weight sums + Counter& counter = *streamCache(id)->get(); + + edm::Handle lheWeightHandle; + bool foundLheWeights = false; + for (auto& token : lheWeightTokens_) { + iEvent.getByToken(token, lheWeightHandle); + if (lheWeightHandle.isValid()) { + foundLheWeights = true; + break; + } + } + //Taken from genweight producer //Added sroychow + // generator information (always available) + auto const& genInfo = iEvent.get(genEventInfoToken_); + const double genWeight = genInfo.weight(); + // table for gen info, always available + auto outGeninfo = std::make_unique(1, "genWeightNEW", true); + outGeninfo->setDoc("generator weight"); + outGeninfo->addColumnValue("", genInfo.weight(), "generator weight", nanoaod::FlatTable::FloatColumn); + iEvent.put(std::move(outGeninfo)); + //this will take care of sum of genWeights + counter.incGenOnly(genWeight); + + 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 const& weightInfos = *luminosityBlockCache(iEvent.getLuminosityBlock().index()); + + auto lheWeightTables = std::make_unique>(); + if (foundLheWeights) { + addWeightGroupToTable(lheWeightTables, "LHE", weightInfos.at(inLHE), lheWeights, counter, genWeight); } - std::shared_ptr 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 - bool foundLheWeights = false; - edm::Handle lheWeightInfoHandle; - for (auto& token : lheWeightInfoTokens_) { - iLumi.getByToken(token, lheWeightInfoHandle); - if (lheWeightInfoHandle.isValid()) { - foundLheWeights = true; - break; - } + addWeightGroupToTable(lheWeightTables, "Gen", weightInfos.at(inGen), genWeights, counter, genWeight); + + iEvent.put(std::move(lheWeightTables)); + //std::cout << "From Event:\n"; + //for(auto& cm : counter.weightSumMap_) + // std::cout << "WeightName:" << cm.first + // << "\t size:" << cm.second.size() + // << "\t First value:" << cm.second.at(0) + // << std::endl; + +} + +void LHEWeightsTableProducer::addWeightGroupToTable(std::unique_ptr>& lheWeightTables, + const char* typeName, + const WeightGroupDataContainer& weightInfos, + WeightsContainer& allWeights, Counter& counter, + double genWeight) const { + std::unordered_map typeCount = {}; + for (auto& type : gen::allWeightTypes) + typeCount[type] = 0; + + for (const auto& groupInfo : weightInfos) { + std::string entryName = typeName; + gen::WeightType weightType = groupInfo.group->weightType(); + std::string name = weightTypeNames_.at(weightType); + std::string label = groupInfo.group->name(); + auto& weights = allWeights.at(groupInfo.index); + label.append("; "); + if (false && weightType == gen::WeightType::kScaleWeights && groupInfo.group->isWellFormed() && + groupInfo.group->nIdsContained() < 10) { + weights = orderedScaleWeights(weights, dynamic_cast(groupInfo.group)); + label.append( + "[1] is mur=0.5 muf=1; [2] is mur=0.5 muf=2; [3] is mur=1 muf=0.5 ;" + " [4] is mur=1 muf=1; [5] is mur=1 muf=2; [6] is mur=2 muf=0.5;" + " [7] is mur=2 muf=1 ; [8] is mur=2 muf=2)"); + } else + label.append(groupInfo.group->description()); + + entryName.append(name); + entryName.append("Weight"); + if (typeCount[weightType] > 0) { + entryName.append("AltSet"); + entryName.append(std::to_string(typeCount[weightType])); } + counter.incLHE(genWeight, weights, entryName); + lheWeightTables->emplace_back(weights.size(), entryName, false); + lheWeightTables->back().addColumn("", weights, label, nanoaod::FlatTable::FloatColumn, lheWeightPrecision_); + typeCount[weightType]++; + } +} - edm::Handle genWeightInfoHandle; - iLumi.getByToken(genWeightInfoToken_, genWeightInfoHandle); + WeightGroupDataContainer LHEWeightsTableProducer::weightDataPerType(edm::Handle& weightsHandle, + gen::WeightType weightType, + int& maxStore) const { + WeightGroupDataContainer group; + if (weightType == gen::WeightType::kPdfWeights && pdfIds_.size() > 0) { + group = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_); + } else + group = weightsHandle->weightGroupsAndIndicesByType(weightType); - std::unordered_map storePerType; - for (size_t i = 0; i < weightgroups_.size(); i++) - storePerType[weightgroups_.at(i)] = maxGroupsPerType_.at(i); + if (maxStore < 0 || static_cast(group.size()) <= maxStore) { + // Modify size in case one type of weight is present in multiple products + maxStore -= group.size(); + return group; + } + return std::vector(group.begin(), group.begin() + maxStore); + } - WeightGroupsToStore weightsToStore; - for (auto weightType : gen::allWeightTypes) { - if (foundLheWeights) { - auto lheWeights = weightDataPerType(lheWeightInfoHandle, weightType, storePerType[weightType]); - weightsToStore.at(inLHE).insert(weightsToStore.at(inLHE).end(), lheWeights.begin(), lheWeights.end()); - } + std::vector LHEWeightsTableProducer::orderedScaleWeights(const std::vector& scaleWeights, + const gen::ScaleWeightGroupInfo* scaleGroup) const { + std::vector weights; + weights.emplace_back(scaleWeights.at(scaleGroup->muR05muF05Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR05muF1Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR05muF2Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR1muF05Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->centralIndex())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR1muF2Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR2muF05Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR2muF1Index())); + weights.emplace_back(scaleWeights.at(scaleGroup->muR2muF2Index())); - auto genWeights = weightDataPerType(genWeightInfoHandle, weightType, storePerType[weightType]); - weightsToStore.at(inGen).insert(weightsToStore.at(inGen).end(), genWeights.begin(), genWeights.end()); - } + return weights; + } - return std::make_shared(weightsToStore); - } +void LHEWeightsTableProducer::streamEndRunSummary(edm::StreamID id, + edm::Run const&, + edm::EventSetup const&, + CounterMap* runCounterMap) const { + std::cout << "<<<<>>>>>\n"; + std::cout << "Map label:" << (*streamCache(id)).active_label << std::endl; + Counter& counter = *streamCache(id)->get(); + for(auto& cm : counter.weightSumMap_) + std::cout << "WeightName:" << cm.first + << "\t size:" << cm.second.size() + << "\t First value:" << cm.second.at(0) + << std::endl; + std::cout << "<<<<>>>>>\n"; + //this takes care for mergeing all the weight sums + runCounterMap->mergeSumMap(*streamCache(id)); +} - WeightGroupDataContainer weightDataPerType(edm::Handle& weightsHandle, - gen::WeightType weightType, - int& maxStore) const { - WeightGroupDataContainer group; - if (weightType == gen::WeightType::kPdfWeights && pdfIds_.size() > 0) { - group = weightsHandle->pdfGroupsWithIndicesByLHAIDs(pdfIds_); - } else - group = weightsHandle->weightGroupsAndIndicesByType(weightType); +void LHEWeightsTableProducer::globalEndRunProduce(edm::Run& iRun, edm::EventSetup const&, CounterMap const* runCounterMap) const { + auto out = std::make_unique(); + + for (auto x : runCounterMap->countermap) { + auto& runCounter = x.second; + std::string label = std::string("_") + x.first; + std::string doclabel = (!x.first.empty()) ? (std::string(", for model label ") + x.first) : ""; + + out->addInt("genEventCountNEW" + label, "event count" + doclabel, runCounter.num_); + out->addFloat("genEventSumwNEW" + label, "sum of gen weights" + doclabel, runCounter.sumw_); + out->addFloat("genEventSumw2NEW" + label, "sum of gen (weight^2)" + doclabel, runCounter.sumw2_); + + double norm = runCounter.sumw_ ? 1.0 / runCounter.sumw_ : 1; + //Sum from map + std::cout << "SUM map size:" << runCounter.weightSumMap_.size() << std::endl; + for(auto& sumw : runCounter.weightSumMap_) { + std::cout << "Adding wsum for:" + << sumw.first << "\t Size:" + << sumw.second.size() + << "\t First value:" << sumw.second.at(0) + << std::endl; - if (maxStore < 0 || static_cast(group.size()) <= maxStore) { - // Modify size in case one type of weight is present in multiple products - maxStore -= group.size(); - return group; + //Normalize with genEventSumw + for(auto& val : sumw.second) val *= norm; + out->addVFloat(sumw.first + "Sumw" + label, + "Sum of genEventWeight *" + sumw.first + "[i]/genEventSumw" + doclabel, + sumw.second) ; } - return std::vector(group.begin(), group.begin() + maxStore); - } - - std::vector orderedScaleWeights(const std::vector& scaleWeights, - const gen::ScaleWeightGroupInfo* scaleGroup) const { - std::vector weights; - weights.push_back(scaleWeights.at(scaleGroup->muR05muF05Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR05muF1Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR05muF2Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR1muF05Index())); - weights.push_back(scaleWeights.at(scaleGroup->centralIndex())); - weights.push_back(scaleWeights.at(scaleGroup->muR1muF2Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR2muF05Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR2muF1Index())); - weights.push_back(scaleWeights.at(scaleGroup->muR2muF2Index())); - - return weights; - } - - // nothing to do here - virtual void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const override {} - - static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - edm::ParameterSetDescription desc; - desc.add>("lheWeights"); - //desc.add>("genWeights"); - desc.add("genWeights"); - desc.add>("weightgroups"); - desc.add>("maxGroupsPerType"); - desc.addOptionalUntracked>("pdfIds"); - desc.add("lheWeightPrecision", -1)->setComment("Number of bits in the mantissa for LHE weights"); - descriptions.addDefault(desc); } + iRun.put(std::move(out)); +} +void LHEWeightsTableProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + edm::ParameterSetDescription desc; + desc.add>("lheWeights"); + desc.add>("lheInfo", std::vector{{"externalLHEProducer"}, {"source"}}) + ->setComment("tag(s) for the LHE information (LHEEventProduct and LHERunInfoProduct)"); + //desc.add>("genWeights"); + desc.add("genWeights"); + desc.add("genEvent", edm::InputTag("generator"))->setComment("tag for the GenEventInfoProduct, to get the main weight"); + desc.add("genLumiInfoHeader", edm::InputTag("generator"))->setComment("tag for the GenLumiInfoProduct, to get the model string"); + desc.add>("weightgroups"); + desc.add>("maxGroupsPerType"); + desc.addOptionalUntracked>("pdfIds"); + desc.add("lheWeightPrecision", -1)->setComment("Number of bits in the mantissa for LHE weights"); + descriptions.addDefault(desc); +} -protected: - const edm::EDGetTokenT lheToken_; - const std::vector> lheWeightTokens_; - const std::vector> lheWeightInfoTokens_; - const edm::EDGetTokenT genWeightToken_; - const edm::EDGetTokenT genWeightInfoToken_; - const std::vector weightgroups_; - const std::vector maxGroupsPerType_; - const std::vector pdfIds_; - const std::unordered_map weightTypeNames_ = { - {gen::WeightType::kScaleWeights, "Scale"}, - {gen::WeightType::kPdfWeights, "Pdf"}, - {gen::WeightType::kMEParamWeights, "MEParam"}, - {gen::WeightType::kPartonShowerWeights, "PartonShower"}, - {gen::WeightType::kUnknownWeights, "Unknown"}, - }; - - //std::unordered_map weightGroupIndices_; - int lheWeightPrecision_; - enum { inLHE, inGen }; -}; #include "FWCore/Framework/interface/MakerMacros.h" DEFINE_FWK_MODULE(LHEWeightsTableProducer);