From bbdd4626eed90e045dfdc0a1826da10673187d4a Mon Sep 17 00:00:00 2001 From: Kenneth Long Date: Mon, 7 Oct 2019 18:09:32 -0400 Subject: [PATCH] Add PDF and PS weights in example --- .../Core/plugins/GenWeightsTestAnalyzer.cc | 54 ++++++++++++++----- .../Core/src/LHEWeightHelper.cc | 4 +- .../testGenWeights_simpleZanalyzer_cfg.py | 5 +- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc b/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc index 7c87b477e962b..aead5ad2baff4 100644 --- a/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc +++ b/GeneratorInterface/Core/plugins/GenWeightsTestAnalyzer.cc @@ -88,13 +88,15 @@ class GenWeightsTestAnalyzer : public edm::one::EDAnalyzer scaleWeightsOrder_; - unsigned int scaleWeightsIndex_; + int scaleWeightsIndex_; + int pdfWeightsIndex_; edm::EDGetTokenT genParticleToken_; edm::EDGetTokenT genJetToken_; edm::EDGetTokenT LHEToken_; edm::EDGetTokenT GenToken_; edm::EDGetTokenT lheWeightToken_; + edm::EDGetTokenT genWeightToken_; edm::EDGetTokenT lheWeightInfoToken_; edm::Service fileservice; @@ -128,12 +130,15 @@ class GenWeightsTestAnalyzer : public edm::one::EDAnalyzer("tag")), isMiniaod_(iConfig.getParameter("miniaod")), + scaleWeightsIndex_(-1), + pdfWeightsIndex_(-1), genParticleToken_(consumes(iConfig.getParameter("genParticleSrc"))), genJetToken_(consumes(iConfig.getParameter("genJetSrc"))), LHEToken_(consumes(iConfig.getParameter("LHESrc"))), GenToken_(consumes(iConfig.getParameter("GenSrc"))), - lheWeightToken_(consumes(edm::InputTag("testWeights"))), - lheWeightInfoToken_(consumes(edm::InputTag("testWeights"))) + lheWeightToken_(consumes(edm::InputTag("testLheWeights"))), + genWeightToken_(consumes(edm::InputTag("testGenWeights"))), + lheWeightInfoToken_(consumes(edm::InputTag("testLheWeights"))) { //now do what ever initialization is needed @@ -333,15 +338,32 @@ void GenWeightsTestAnalyzer::setup_variables(const edm::Event& iEvent) { } std::vector GenWeightsTestAnalyzer::setup_weights(const edm::Event& iEvent) { edm::Handle lheWeightHandle; + edm::Handle genWeightHandle; iEvent.getByToken(lheWeightToken_, lheWeightHandle); - const GenWeightProduct * lheWeights = lheWeightHandle.product(); - WeightsContainer weights = lheWeights->weights(); - auto scaleWeights = weights.at(scaleWeightsIndex_); + iEvent.getByToken(genWeightToken_, genWeightHandle); + const GenWeightProduct * lheWeightProduct = lheWeightHandle.product(); + const GenWeightProduct * genWeightProduct = genWeightHandle.product(); + WeightsContainer lheWeights = lheWeightProduct->weights(); + WeightsContainer genWeights = genWeightProduct->weights(); + + auto scaleWeights = scaleWeightsIndex_ >= 0 ? lheWeights.at(scaleWeightsIndex_) : std::vector(); + auto pdfWeights = pdfWeightsIndex_ > 0 ? lheWeights.at(pdfWeightsIndex_) : std::vector(); std::vector keepWeights; for(auto i : scaleWeightsOrder_){ - keepWeights.push_back(scaleWeights[i]); + keepWeights.push_back(scaleWeights.at(i)); } + + for(auto& weight : pdfWeights){ + keepWeights.push_back(weight); + } + + if (genWeights.size() > 0) { + for(auto& weight : genWeights.front()){ + keepWeights.push_back(weight); + } + } + return keepWeights; } @@ -400,7 +422,10 @@ GenWeightsTestAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, iLumi.getByToken(lheWeightInfoToken_, lheWeightInfoHandle); // Should add a search by name function - scaleWeightsIndex_ = lheWeightInfoHandle->weightGroupIndicesByType(gen::kScaleWeights).front(); + auto allScaleWeights = lheWeightInfoHandle->weightGroupIndicesByType(gen::kScaleWeights); + if (allScaleWeights.size() > 0) + scaleWeightsIndex_ = allScaleWeights.front(); + auto scaleWeights = static_cast( lheWeightInfoHandle->orderedWeightGroupInfo(scaleWeightsIndex_)); // nano ordering of mur=0.5 muf=0.5 ; [1] is mur=0.5 muf=1 ; [2] is mur=0.5 muf=2 ; [3] is mur=1 muf=0.5 ; @@ -416,11 +441,14 @@ GenWeightsTestAnalyzer::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, scaleWeightsOrder_.push_back(scaleWeights->muR2muF1Index()); scaleWeightsOrder_.push_back(scaleWeights->muR2muF2Index()); - //auto pdfSets = lheWeightInfoHandle->weightGroupIndicesByType(gen::kPdfWeights); - - //for (const auto& pdfSet : pdfSets) { - // keepPdfIndex_ = pdfSet.indexOfLhapdfId(KEEP_LHAPDFID_); - // if (keepPdfIdex = -1) + auto pdfGroups = lheWeightInfoHandle->weightGroupsByType(gen::kPdfWeights); + auto ct14Set = std::find_if(pdfGroups.begin(), pdfGroups.end(), + [] (gen::WeightGroupInfo* group) { + auto pdfGroup = dynamic_cast(group); + return pdfGroup->containsLhapdfId(1300); + }); + if (ct14Set != pdfGroups.end()) + pdfWeightsIndex_ = std::distance(pdfGroups.begin(), ct14Set); } //define this as a plug-in diff --git a/GeneratorInterface/Core/src/LHEWeightHelper.cc b/GeneratorInterface/Core/src/LHEWeightHelper.cc index f63120b427400..73fb67b19b195 100644 --- a/GeneratorInterface/Core/src/LHEWeightHelper.cc +++ b/GeneratorInterface/Core/src/LHEWeightHelper.cc @@ -135,8 +135,8 @@ namespace gen { else if (isAWeight(headerLine)) { currWeightAttributeMap_.clear(); // This shouldn't really happen, but perhaps we find weights outside of weight groups? - if (currGroupAttributeMap_.empty()) - weightGroups_.push_back(std::make_unique("Unknown")); + //if (weightGroups_.empty()) + // weightGroups_.push_back(std::make_unique("Unknown")); currWeightAttributeMap_ = getAttributeMap(headerLine); std::string content = currWeightAttributeMap_["content"]; diff --git a/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py b/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py index 0a6bfe3336233..4e364b0aa2557 100644 --- a/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py +++ b/GeneratorInterface/Core/test/testGenWeights_simpleZanalyzer_cfg.py @@ -59,7 +59,8 @@ def commandline(): fileNames = cms.untracked.vstring( options.inputFiles ) ) -process.testWeights = cms.EDProducer("LHEWeightProductProducer") +process.testLheWeights = cms.EDProducer("LHEWeightProductProducer") +process.testGenWeights = cms.EDProducer("GenWeightProductProducer") process.demo = cms.EDAnalyzer('GenWeightsTestAnalyzer', tag = cms.string(options.tag), @@ -73,5 +74,5 @@ def commandline(): process.TFileService = cms.Service("TFileService", fileName = cms.string("analysis_{TAG}.root".format(TAG=options.tag)) ) -process.p = cms.Path(process.testWeights*process.demo) +process.p = cms.Path(process.testLheWeights*process.testGenWeights*process.demo)