Skip to content

Commit

Permalink
Incorporate changes to make OwnVector work
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Sep 20, 2019
1 parent 07eb97a commit 5668f0d
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 50 deletions.
37 changes: 20 additions & 17 deletions GeneratorInterface/LHEInterface/plugins/ExternalLHEProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ class ExternalLHEProducer : public edm::one::EDProducer<edm::BeginRunProducer,
std::unique_ptr<lhef::LHEReader> reader_;
std::shared_ptr<lhef::LHERunInfo> runInfoLast;
std::shared_ptr<lhef::LHERunInfo> runInfo;
std::unique_ptr<LHEWeightInfoProduct> weightInfoProduct_;
std::shared_ptr<lhef::LHEEvent> partonLevel;
boost::ptr_deque<LHERunInfoProduct> runInfoProducts;
bool wasMerged;
edm::OwnVector<gen::WeightGroupInfo> weightGroups_;

class FileCloseSentry : private boost::noncopyable {
public:
Expand Down Expand Up @@ -165,7 +165,7 @@ ExternalLHEProducer::ExternalLHEProducer(const edm::ParameterSet& iConfig) :
produces<LHEWeightProduct>();
produces<LHERunInfoProduct, edm::Transition::BeginRun>();
produces<LHERunInfoProduct, edm::Transition::EndRun>();
produces<LHEWeightInfoProduct, edm::Transition::BeginRun>();
produces<LHEWeightInfoProduct, edm::Transition::EndRun>();
}


Expand Down Expand Up @@ -208,17 +208,18 @@ ExternalLHEProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
boost::bind(&LHEEventProduct::addWeight,
product.get(), _1));

std::unique_ptr<LHEWeightProduct> weightProduct(new LHEWeightProduct);
weightProduct->setNumWeightSets(weightGroups_.size());
auto weightProduct = std::make_unique<LHEWeightProduct>();
weightProduct->setNumWeightSets(weightInfoProduct_->numberOfGroups());
int weightGroupIndex = 0;
int weightNum = 0;
for (const auto& weight : partonLevel->weights()) {
weightGroupIndex = findWeightGroup(weight.id, weightNum, weightGroupIndex);
if (weightGroupIndex < 0 || weightGroupIndex >= static_cast<int>(weightGroups_.size())) {
continue;
if (weightGroupIndex < 0 || weightGroupIndex >= weightInfoProduct_->numberOfGroups()) {
// Needs to be properly handled
throw std::range_error("Unmatched weight");
}
auto group = weightGroups_[weightGroupIndex];
int entry = group.weightVectorEntry(weight.id, weightNum);
auto* group = weightInfoProduct_->orderedWeightGroupInfo(weightGroupIndex);
int entry = group->weightVectorEntry(weight.id, weightNum);
weightProduct->addWeight(weight.wgt, weightGroupIndex, entry);
weightNum++;
}
Expand Down Expand Up @@ -371,16 +372,15 @@ ExternalLHEProducer::beginRunProduce(edm::Run& run, edm::EventSetup const& es)

run.put(std::move(product));

std::unique_ptr<LHEWeightInfoProduct> weightInfoProduct(new LHEWeightInfoProduct);
weightInfoProduct_ = std::make_unique<LHEWeightInfoProduct>();

LHEWeightGroupReaderHelper reader;
//reader.parseLHEFile(LHEfilename);
reader.parseWeightGroupsFromHeader(runInfo->findHeader("initrwgt"));

for (auto weightGroup : reader.getWeightGroups())
weightInfoProduct->addWeightGroupInfo(weightGroup);
weightGroups_ = weightInfoProduct->allWeightGroupsInfo();
run.put(std::move(weightInfoProduct));
for (auto& weightGroup : reader.getWeightGroups()) {
weightInfoProduct_->addWeightGroupInfo(weightGroup.clone());
}

runInfo.reset();
}
Expand All @@ -405,6 +405,7 @@ ExternalLHEProducer::endRunProduce(edm::Run& run, edm::EventSetup const& es)
}

reader_.reset();
run.put(std::move(weightInfoProduct_));

if (unlink(outputFile_.c_str())) {
throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_ << " (errno=" << errno << ", " << strerror(errno) << ").";
Expand Down Expand Up @@ -540,17 +541,19 @@ int ExternalLHEProducer::findWeightGroup(std::string wgtId, int weightIndex, int
// Start search at previous index, under expectation of ordered weights
previousGroupIndex = previousGroupIndex >=0 ? previousGroupIndex : 0;
for (int index = previousGroupIndex;
index < std::min(index+1, static_cast<int>(weightGroups_.size())); index++) {
gen::WeightGroupInfo& weightGroup = weightGroups_[previousGroupIndex];
index < std::min(index+1, static_cast<int>(weightInfoProduct_->numberOfGroups())); index++) {
const gen::WeightGroupInfo* weightGroup = weightInfoProduct_->orderedWeightGroupInfo(index);
// index < std::min(index+1, static_cast<int>(weightGroups_.size())); index++) {
//gen::WeightGroupInfo& weightGroup = weightGroups_[previousGroupIndex];
// Fast search assuming order is not perturbed outside of weight group
if (weightGroup.indexInRange(weightIndex) && weightGroup.containsWeight(wgtId, weightIndex)) {
if (weightGroup->indexInRange(weightIndex) && weightGroup->containsWeight(wgtId, weightIndex)) {
return static_cast<int>(index);
}
}

// Fall back to unordered search
int counter = 0;
for (auto weightGroup : weightGroups_) {
for (auto weightGroup : weightInfoProduct_->allWeightGroupsInfo()) {
if (weightGroup.containsWeight(wgtId, weightIndex))
return counter;
counter++;
Expand Down
4 changes: 2 additions & 2 deletions GeneratorInterface/LHEInterface/plugins/LHESource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void LHESource::putWeightInfoProduct(edm::RunPrincipal& iRunPrincipal) {
);
cenPdfInfo.setWeightType(gen::kPdfWeights);

product->addWeightGroupInfo(scaleInfo);
product->addWeightGroupInfo(cenPdfInfo);
product->addWeightGroupInfo(&scaleInfo);
product->addWeightGroupInfo(&cenPdfInfo);
std::unique_ptr<edm::WrapperBase> rdp(new edm::Wrapper<LHEWeightInfoProduct>(std::move(product)));
iRunPrincipal.put(lheProvenanceHelper_.weightProductBranchDescription_, std::move(rdp));
}
Expand Down
9 changes: 9 additions & 0 deletions GeneratorInterface/LHEInterface/test/testWeights.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from DataFormats.FWLite import Events,Handle,Runs
import ROOT
source = "externalLHEProducer"

#for filename in ["HIG-RunIIFall18wmLHEGS-00509.root"," HIG-RunIIFall18wmLHEGS-00509_ordered.root","HIG-RunIIFall18wmLHEGS-00509_unordered.root"]:
Expand All @@ -19,6 +20,14 @@
for j, weights in enumerate(weightInfo.weights()):
print "-"*10, "Looking at entry", j, "length is", len(weights),"-"*10
matching = weightInfoProd.orderedWeightGroupInfo(j)
print matching
if matching.weightType() == 1:
print " muR1muF05Index", matching.muR1muF05Index()
print " muR1muF1Index", matching.centralIndex()
print " muR2muF2Index", matching.muR2muF2Index()
print " muR05muF05Index", matching.muR05muF05Index()
else:
print "uncertaintyType", "Hessian" if matching.uncertaintyType() == ROOT.gen.kHessianUnc else "MC"
print "Weights length?", len(weights), "Contained ids lenths?", len(matching.containedIds())
print "-"*80
for i,weight in enumerate(weights):
Expand Down
9 changes: 0 additions & 9 deletions GeneratorInterface/LHEInterface/test/test_Weights_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@

# import of standard configurations
process.load('Configuration.StandardSequences.Services_cff')
process.load('SimGeneral.HepPDTESSource.pythiapdt_cfi')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('Configuration.EventContent.EventContent_cff')
process.load('SimGeneral.MixingModule.mixNoPU_cfi')
process.load('Configuration.StandardSequences.GeometryRecoDB_cff')
process.load('Configuration.StandardSequences.MagneticField_cff')
process.load('Configuration.StandardSequences.EndOfProcess_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')

process.maxEvents = cms.untracked.PSet(
input = cms.untracked.int32(10)
Expand Down Expand Up @@ -54,10 +49,6 @@

# Additional output definition

# Other statements
from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, '102X_upgrade2018_realistic_v11', '')

process.externalLHEProducer = cms.EDProducer("ExternalLHEProducer",
#args = cms.vstring('/afs/cern.ch/user/k/kelong/work/public/DummyGridpacks/WLLJJ_WToLNu_EWK_4F_MLL-60_slc6_amd64_gcc481_CMSSW_7_1_30_tarball_Dummy.tgz'),
args = cms.vstring('/afs/cern.ch/user/k/kelong/work/public/DummyGridpacks/ZZ_4L_NNPDF30_13TeV_tarballDummy.tar.gz'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class LHEWeightInfoProduct {
LHEWeightInfoProduct& operator=(LHEWeightInfoProduct &&other);

const edm::OwnVector<gen::WeightGroupInfo>& allWeightGroupsInfo() const;
const gen::WeightGroupInfo& containingWeightGroupInfo(int index) const;
const gen::WeightGroupInfo& orderedWeightGroupInfo(int index) const;
void addWeightGroupInfo(gen::WeightGroupInfo& info);
const gen::WeightGroupInfo* containingWeightGroupInfo(int index) const;
const gen::WeightGroupInfo* orderedWeightGroupInfo(int index) const;
void addWeightGroupInfo(gen::WeightGroupInfo* info);
const int numberOfGroups() const { return weightGroupsInfo_.size(); }

private:
edm::OwnVector<gen::WeightGroupInfo> weightGroupsInfo_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace gen {
}
virtual ~PdfWeightGroupInfo() override {}
void copy(const PdfWeightGroupInfo &other);
PdfWeightGroupInfo* clone() const;
virtual PdfWeightGroupInfo* clone() const override;

void setUncertaintyType(PdfUncertaintyType uncertaintyType) { uncertaintyType_ = uncertaintyType; }
void setHasAlphasVariations(bool hasAlphasVars) { hasAlphasVars_ = hasAlphasVars; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace gen {
}
virtual ~ScaleWeightGroupInfo() override {}
void copy(const ScaleWeightGroupInfo &other);
ScaleWeightGroupInfo* clone() const;
virtual ScaleWeightGroupInfo* clone() const override;

void setMuRMuFIndex(WeightMetaInfo info, float muR, float muF);
void addContainedId(int weightEntry, std::string id, std::string label, float muR, float muF);
Expand Down
12 changes: 6 additions & 6 deletions SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace gen {
}
virtual ~WeightGroupInfo() {};
void copy(const WeightGroupInfo &other);
WeightGroupInfo* clone() const;
WeightMetaInfo weightMetaInfo(int weightEntry);
WeightMetaInfo weightMetaInfo(std::string wgtId);
int weightVectorEntry(const std::string& wgtId);
int containsWeight(const std::string& wgtId, int weightEntry);
int weightVectorEntry(const std::string& wgtId, int weightEntry);
virtual WeightGroupInfo* clone() const;
WeightMetaInfo weightMetaInfo(int weightEntry) const;
WeightMetaInfo weightMetaInfo(std::string wgtId) const;
int weightVectorEntry(const std::string& wgtId) const;
int containsWeight(const std::string& wgtId, int weightEntry) const;
int weightVectorEntry(const std::string& wgtId, int weightEntry) const;
void addContainedId(int weightEntry, std::string id, std::string label);
std::vector<WeightMetaInfo> containedIds() const;
bool indexInRange(int index) const;
Expand Down
10 changes: 5 additions & 5 deletions SimDataFormats/GeneratorProducts/src/LHEWeightInfoProduct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ const edm::OwnVector<gen::WeightGroupInfo>& LHEWeightInfoProduct::allWeightGroup
return weightGroupsInfo_;
}

const gen::WeightGroupInfo& LHEWeightInfoProduct::containingWeightGroupInfo(int index) const {
const gen::WeightGroupInfo* LHEWeightInfoProduct::containingWeightGroupInfo(int index) const {
for (const auto& weightGroup : weightGroupsInfo_) {
if (weightGroup.indexInRange(index))
return weightGroup;
return &weightGroup;
}
throw std::domain_error("Failed to find containing weight group");
}

const gen::WeightGroupInfo& LHEWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
const gen::WeightGroupInfo* LHEWeightInfoProduct::orderedWeightGroupInfo(int weightGroupIndex) const {
if (weightGroupIndex >= static_cast<int>(weightGroupsInfo_.size()))
throw std::range_error("Weight index out of range!");
return weightGroupsInfo_[weightGroupIndex];
return &weightGroupsInfo_[weightGroupIndex];
}

void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo& info) {
void LHEWeightInfoProduct::addWeightGroupInfo(gen::WeightGroupInfo* info) {
weightGroupsInfo_.push_back(info);
}
1 change: 1 addition & 0 deletions SimDataFormats/GeneratorProducts/src/PdfWeightGroupInfo.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h"
#include <iostream>

namespace gen {
void PdfWeightGroupInfo::copy(const PdfWeightGroupInfo &other) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h"
#include <stdexcept>
#include <iostream>

namespace gen {
void ScaleWeightGroupInfo::copy(const ScaleWeightGroupInfo &other) {
Expand Down
11 changes: 6 additions & 5 deletions SimDataFormats/GeneratorProducts/src/WeightGroupInfo.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string>
#include <algorithm>
#include "SimDataFormats/GeneratorProducts/interface/WeightGroupInfo.h"
#include <iostream>

namespace gen {
void WeightGroupInfo::copy(const WeightGroupInfo &other) {
Expand All @@ -16,24 +17,24 @@ namespace gen {
return new WeightGroupInfo(*this);
}

WeightMetaInfo WeightGroupInfo::weightMetaInfo(int weightEntry) {
WeightMetaInfo WeightGroupInfo::weightMetaInfo(int weightEntry) const {
return idsContained_.at(weightEntry);
}

WeightMetaInfo WeightGroupInfo::weightMetaInfo(std::string wgtId) {
WeightMetaInfo WeightGroupInfo::weightMetaInfo(std::string wgtId) const {
int weightEntry = weightVectorEntry(wgtId);
return idsContained_.at(weightEntry);
}

int WeightGroupInfo::weightVectorEntry(const std::string& wgtId) {
int WeightGroupInfo::weightVectorEntry(const std::string& wgtId) const {
return weightVectorEntry(wgtId, 0);
}

int WeightGroupInfo::containsWeight(const std::string& wgtId, int weightEntry) {
int WeightGroupInfo::containsWeight(const std::string& wgtId, int weightEntry) const {
return weightVectorEntry(wgtId, weightEntry) != -1;
}

int WeightGroupInfo::weightVectorEntry(const std::string& wgtId, int weightEntry) {
int WeightGroupInfo::weightVectorEntry(const std::string& wgtId, int weightEntry) const {
int entry = -1;
if (!indexInRange(weightEntry)) {
size_t orderedEntry = weightEntry - firstId_;
Expand Down
9 changes: 8 additions & 1 deletion SimDataFormats/GeneratorProducts/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,21 @@
<class name="std::vector<gen::PdfWeightGroupInfo*>"/>
<class name="std::vector<gen::ScaleWeightGroupInfo>"/>
<class name="std::vector<gen::ScaleWeightGroupInfo*>"/>
<class name="edm::OwnVector<gen::WeightGroupInfo>"/>
<class name="edm::Wrapper<gen::WeightGroupInfo>"/>
<class name="edm::Wrapper<gen::PdfWeightGroupInfo>"/>
<class name="edm::Wrapper<gen::ScaleWeightGroupInfo>"/>
<class name="edm::ClonePolicy<gen::WeightGroupInfo>"/>
<class name="edm::OwnVector<gen::WeightGroupInfo, edm::ClonePolicy<gen::WeightGroupInfo> >"/>
<class name="edm::Wrapper<edm::OwnVector<gen::WeightGroupInfo, edm::ClonePolicy<gen::WeightGroupInfo> >"/>
<class name="edm::Wrapper&lt;LHERunInfoProduct&gt;"/>
<class name="edm::Wrapper&lt;LHEWeightInfoProduct&gt;"/>
<class name="edm::Wrapper&lt;LHEWeightProduct&gt;"/>
<class name="edm::Wrapper&lt;LHEEventProduct&gt;"/>
<exclusion>
<class name="edm::OwnVector<gen::WeightGroupInfo, edm::ClonePolicy<gen::WeightGroupInfo> >">
<method name="sort" />
</class>
</exclusion>

<!--needed for backward compatibility between HepMC 2.06.xx and 2.05.yy-->
<ioread sourceClass = "HepMC::Polarization" version="[1-10]" targetClass="HepMC::Polarization" source = "" target="m_defined">
Expand Down

0 comments on commit 5668f0d

Please sign in to comment.