Skip to content

Commit

Permalink
changed LHEsource so it compiles, running tests now
Browse files Browse the repository at this point in the history
added scientific notation for numbers so values are correct

added a few more test statements. Need to think of future needs

Add testing suite (will remove/squash commits right before merge)

Change pdfSetsInfo to be read in from LHAPDF path
Still lingering question of how to decide pdf type
This can be resolved in the new function "setupPdfSetsInfo"

Add check if orphan weight is pdf and fix xml tag swap (needs review)

Allow reading '>' as '>' and fix bug with first weight w/o group
As for the first weight error: if the first weight doesn't have a
weight group, the code would not add it to the list of weights because
of currentGroupName and the first weight both having a trivial "" as
their name. Thats the reason for the currentGroupName being set to
"None" for initialization

Fix space issue in regex and moved unchaning variables outside for

Clang format
  • Loading branch information
dteague authored and kdlong committed May 20, 2020
1 parent ba650b1 commit 0e63151
Show file tree
Hide file tree
Showing 49 changed files with 16,505 additions and 1,216 deletions.
17 changes: 8 additions & 9 deletions GeneratorInterface/Core/interface/GenWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
#include <tinyxml2.h>

namespace gen {
class GenWeightHelper : public WeightHelper {
public:
GenWeightHelper();
class GenWeightHelper : public WeightHelper {
public:
GenWeightHelper();

void parseWeightGroupsFromNames(std::vector<std::string> weightNames);
private:
};
}

#endif
void parseWeightGroupsFromNames(std::vector<std::string> weightNames);

private:
};
} // namespace gen

#endif
26 changes: 14 additions & 12 deletions GeneratorInterface/Core/interface/LHEWeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
#include <tinyxml2.h>

namespace gen {
class LHEWeightHelper : public WeightHelper {
public:
LHEWeightHelper() : WeightHelper() {};
void setHeaderLines(std::vector<std::string> headerLines);
void parseWeights();
void buildGroups();
std::unique_ptr<WeightGroupInfo> buildGroup(const ParsedWeight& weight);
private:
std::vector<std::string> headerLines_;
};
}
class LHEWeightHelper : public WeightHelper {
public:
LHEWeightHelper() : WeightHelper(){};
void setHeaderLines(std::vector<std::string> headerLines);
void parseWeights();
void buildGroups();
bool isConsistent();
void swapHeaders();
std::unique_ptr<WeightGroupInfo> buildGroup(ParsedWeight& weight);

#endif
private:
std::vector<std::string> headerLines_;
};
} // namespace gen

#endif
100 changes: 52 additions & 48 deletions GeneratorInterface/Core/interface/WeightHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,63 @@
#include "SimDataFormats/GeneratorProducts/interface/PdfWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/ScaleWeightGroupInfo.h"
#include "SimDataFormats/GeneratorProducts/interface/MEParamWeightGroupInfo.h"
#include "LHAPDF/LHAPDF.h"
#include <boost/algorithm/string.hpp>
#include <fstream>

namespace gen {
struct PdfSetInfo {
std::string name;
int lhapdfId;
PdfUncertaintyType uncertaintyType;
};
struct PdfSetInfo {
std::string name;
int lhapdfId;
PdfUncertaintyType uncertaintyType;
};

struct ParsedWeight {
std::string id;
size_t index;
std::string groupname;
std::string content;
std::unordered_map<std::string, std::string> attributes;
};

class WeightHelper {
public:
WeightHelper();
edm::OwnVector<gen::WeightGroupInfo> weightGroups() {
return weightGroups_;
}
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<gen::WeightsInfo>, float w0);
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<double>, float w0);
void setModel(std::string model) { model_ = model; }
int addWeightToProduct(std::unique_ptr<GenWeightProduct>& product, double weight, std::string name, int weightNum, int groupIndex);
int findContainingWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex);
protected:
std::string model_;
std::vector<ParsedWeight> parsedWeights_;
const std::vector<PdfSetInfo> pdfSetsInfo;
std::map<std::string, std::string> currWeightAttributeMap_;
std::map<std::string, std::string> currGroupAttributeMap_;
edm::OwnVector<gen::WeightGroupInfo> weightGroups_;
bool isScaleWeightGroup(const ParsedWeight& weight);
bool isMEParamWeightGroup(const ParsedWeight& weight);
bool isPdfWeightGroup(const ParsedWeight& weight);
void updateScaleInfo(const ParsedWeight& weight);
void updatePdfInfo(const ParsedWeight& weight);
void splitPdfGroups();
std::string searchAttributes(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByTag(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByRegex(const std::string& label, const ParsedWeight& weight) const;
struct ParsedWeight {
std::string id;
size_t index;
std::string groupname;
std::string content;
std::unordered_map<std::string, std::string> attributes;
};

class WeightHelper {
public:
WeightHelper();
edm::OwnVector<gen::WeightGroupInfo> weightGroups() { return weightGroups_; }
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<gen::WeightsInfo>, float w0);
std::unique_ptr<GenWeightProduct> weightProduct(std::vector<double>, float w0);
void setModel(std::string model) { model_ = model; }
int addWeightToProduct(
std::unique_ptr<GenWeightProduct>& product, double weight, std::string name, int weightNum, int groupIndex);
int findContainingWeightGroup(std::string wgtId, int weightIndex, int previousGroupIndex);

// Possible names for the same thing
const std::unordered_map<std::string, std::vector<std::string>> attributeNames_ = {
{"muf", {"muR", "MUR", "muf","facscfact"}},
{"mur", {"muF", "MUF", "mur","renscfact"}},
{"pdf", {"PDF", "PDF set", "lhapdf", "pdf", "pdf set", "pdfset"}}
};
protected:
std::string model_;
std::vector<ParsedWeight> parsedWeights_;
const std::vector<PdfSetInfo> pdfSetsInfo;
std::map<std::string, std::string> currWeightAttributeMap_;
std::map<std::string, std::string> currGroupAttributeMap_;
edm::OwnVector<gen::WeightGroupInfo> weightGroups_;
bool isScaleWeightGroup(const ParsedWeight& weight);
bool isMEParamWeightGroup(const ParsedWeight& weight);
bool isPdfWeightGroup(const ParsedWeight& weight);
bool isOrphanPdfWeightGroup(ParsedWeight& weight);
void updateScaleInfo(const ParsedWeight& weight);
void updatePdfInfo(const ParsedWeight& weight);
void splitPdfGroups();
std::vector<PdfSetInfo> setupPdfSetsInfo();
std::string searchAttributes(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByTag(const std::string& label, const ParsedWeight& weight) const;
std::string searchAttributesByRegex(const std::string& label, const ParsedWeight& weight) const;

// Possible names for the same thing
const std::unordered_map<std::string, std::vector<std::string>> attributeNames_ = {
{"muf", {"muF", "MUF", "muf", "facscfact"}},
{"mur", {"muR", "MUR", "mur", "renscfact"}},
{"pdf", {"PDF", "PDF set", "lhapdf", "pdf", "pdf set", "pdfset"}},
//{"dyn", {"DYN_SCALE", "dyn_scale_choice"}},
};
}
};
} // namespace gen

#endif

66 changes: 29 additions & 37 deletions GeneratorInterface/Core/plugins/GenWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GenWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit
public:
explicit GenWeightProductProducer(const edm::ParameterSet& iConfig);
~GenWeightProductProducer() override;

private:
std::vector<std::string> weightNames_;
gen::GenWeightHelper weightHelper_;
Expand All @@ -44,24 +44,19 @@ class GenWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit
//
// constructors and destructor
//
GenWeightProductProducer::GenWeightProductProducer(const edm::ParameterSet& iConfig) :
genLumiInfoToken_(consumes<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genInfo"))),
genEventToken_(consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("genInfo"))),
genLumiInfoHeadTag_(mayConsume<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genLumiInfoHeader")))
{
GenWeightProductProducer::GenWeightProductProducer(const edm::ParameterSet& iConfig)
: genLumiInfoToken_(consumes<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genInfo"))),
genEventToken_(consumes<GenEventInfoProduct>(iConfig.getParameter<edm::InputTag>("genInfo"))),
genLumiInfoHeadTag_(
mayConsume<GenLumiInfoHeader, edm::InLumi>(iConfig.getParameter<edm::InputTag>("genLumiInfoHeader"))) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
}


GenWeightProductProducer::~GenWeightProductProducer()
{
}

GenWeightProductProducer::~GenWeightProductProducer() {}

// ------------ method called to produce the data ------------
void
GenWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
void GenWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
edm::Handle<GenEventInfoProduct> genEventInfo;
iEvent.getByToken(genEventToken_, genEventInfo);

Expand All @@ -70,30 +65,27 @@ GenWeightProductProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSe
iEvent.put(std::move(weightProduct));
}

void
GenWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& iLumi, edm::EventSetup const& iSetup) {
edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
iLumi.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
if (genLumiInfoHead.isValid()) {
std::string label = genLumiInfoHead->configDescription();
boost::replace_all(label,"-","_");
weightHelper_.setModel(label);
}

if (weightNames_.size() == 0) {
edm::Handle<GenLumiInfoHeader> genLumiInfoHandle;
iLumi.getByToken(genLumiInfoToken_, genLumiInfoHandle);

weightNames_ = genLumiInfoHandle->weightNames();
weightHelper_.parseWeightGroupsFromNames(weightNames_);
}
auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
}
iLumi.put(std::move(weightInfoProduct));
void GenWeightProductProducer::beginLuminosityBlockProduce(edm::LuminosityBlock& iLumi, edm::EventSetup const& iSetup) {
edm::Handle<GenLumiInfoHeader> genLumiInfoHead;
iLumi.getByToken(genLumiInfoHeadTag_, genLumiInfoHead);
if (genLumiInfoHead.isValid()) {
std::string label = genLumiInfoHead->configDescription();
boost::replace_all(label, "-", "_");
weightHelper_.setModel(label);
}

if (weightNames_.size() == 0) {
edm::Handle<GenLumiInfoHeader> genLumiInfoHandle;
iLumi.getByToken(genLumiInfoToken_, genLumiInfoHandle);

weightNames_ = genLumiInfoHandle->weightNames();
weightHelper_.parseWeightGroupsFromNames(weightNames_);
}
auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
}
iLumi.put(std::move(weightInfoProduct));
}

DEFINE_FWK_MODULE(GenWeightProductProducer);


90 changes: 39 additions & 51 deletions GeneratorInterface/Core/plugins/LHEWeightProductProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@

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

class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer,
edm::one::WatchRuns> {
class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosityBlockProducer, edm::one::WatchRuns> {
public:
explicit LHEWeightProductProducer(const edm::ParameterSet& iConfig);
~LHEWeightProductProducer() override;

private:
gen::LHEWeightHelper weightHelper_;
std::string lheLabel_;
Expand All @@ -43,75 +42,64 @@ class LHEWeightProductProducer : public edm::one::EDProducer<edm::BeginLuminosit
void beginLuminosityBlockProduce(edm::LuminosityBlock& lumi, edm::EventSetup const& es) override;
void beginRun(edm::Run const& run, edm::EventSetup const& es) override;
void endRun(edm::Run const& run, edm::EventSetup const& es) override;

};

// 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_))
{
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_)) {
produces<GenWeightProduct>();
produces<GenWeightInfoProduct, edm::Transition::BeginLuminosityBlock>();
}


LHEWeightProductProducer::~LHEWeightProductProducer()
{
}

LHEWeightProductProducer::~LHEWeightProductProducer() {}

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

edm::Handle<LHEEventProduct> lheEventInfo;
iEvent.getByToken(lheEventToken_, lheEventInfo);
// Read weights from LHEEventProduct
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);

typedef std::vector<LHERunInfoProduct::Header>::const_iterator header_cit;
LHERunInfoProduct::Header headerWeightInfo;
for (header_cit iter=lheRunInfoHandle->headers_begin(); iter!=lheRunInfoHandle->headers_end(); iter++) {
if (iter->tag() == "initrwgt") {
headerWeightInfo = *iter;
break;
}
void LHEWeightProductProducer::beginRun(edm::Run const& run, edm::EventSetup const& es) {
edm::Handle<LHERunInfoProduct> lheRunInfoHandle;
run.getByLabel(lheLabel_, lheRunInfoHandle);

typedef std::vector<LHERunInfoProduct::Header>::const_iterator header_cit;
LHERunInfoProduct::Header headerWeightInfo;
for (header_cit iter = lheRunInfoHandle->headers_begin(); iter != lheRunInfoHandle->headers_end(); iter++) {
if (iter->tag() == "initrwgt") {
headerWeightInfo = *iter;
break;
}
}

weightHelper_.setHeaderLines(headerWeightInfo.lines());
weightHelper_.setHeaderLines(headerWeightInfo.lines());
}

void
LHEWeightProductProducer::endRun(edm::Run const& run, edm::EventSetup const& es) { }

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

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
}
lumi.put(std::move(weightInfoProduct));
void LHEWeightProductProducer::endRun(edm::Run const& run, edm::EventSetup const& es) {}

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

auto weightInfoProduct = std::make_unique<GenWeightInfoProduct>();
for (auto& weightGroup : weightHelper_.weightGroups()) {
weightInfoProduct->addWeightGroupInfo(weightGroup.clone());
}
lumi.put(std::move(weightInfoProduct));
}

DEFINE_FWK_MODULE(LHEWeightProductProducer);

Loading

0 comments on commit 0e63151

Please sign in to comment.